SpringBoot 整合 Swagger2

   Swagger可以根据业务代码自动生成相关的api接口文档,尤其适用于restful风格的项目。开发人员几乎不用专门维护rest api,这个框架可以自动为你的业务代码生成restfut风格的api,而且还提供相应的测试界面,自动显示json格式的响应。大大方便了后台开发人员与前端的沟通与联调成本。

1.pom.xml中增加Swagger2依赖

    <!-- Swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>

2.创建一个Swagger2的配置类

package com.wheat.springboot.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

	@Bean
	public Docket accessToken() {
		return new Docket(DocumentationType.SWAGGER_2)
			    .groupName("api")// 定义组
			    .select() // 选择那些路径和 api 会生成 document
			    .apis(RequestHandlerSelectors.basePackage("com.wheat.springboot.controller")) // 拦截的包路径
			    .paths(PathSelectors.regex("/*/.*"))// 拦截的接口路径
			    .build() // 创建
			    .apiInfo(apiInfo()); // 配置说明
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()//
				.title("Spring Boot 初体验")// 标题
				.description("spring boot 整合Swagge2 相关内容")// 描述
				.termsOfServiceUrl("http://www.baidu.com")//
				.contact(new Contact("super_wheat", "http://www.baidu.com", "[email protected]"))// 联系
				.version("1.0")// 版本
				.build();
	}
}

3.在 Controller 类上使用 Swagger2 注解

package com.wheat.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(value = "helloworld测试", tags = { "测试接口" })
@RestController
public class TestController {
	@Value("${ds.userName}")
	private String userName;
	@Value("${ds.password}")
	private String password;
	@Autowired
	private Environment environment;
	  
	@ApiOperation("简单的helloworld测试,试验@Value标签的使用")
	@ApiImplicitParam(name = "name", value = "用户名", dataType = "string", paramType = "query")
	@RequestMapping(value="/helloworld",method=RequestMethod.GET)
	public String helloWorld(){
		System.out.println("userName = " + this.userName);
		System.out.println("password = " + this.environment.getProperty("ds.password"));
		return String.format("userName = %s , password = %s !!!", this.userName,this.password);
	}
}

4.启动项目,浏览器查看接口情况

访问链接:http://localhost:8080/swagger-ui.html

 注意:controller方法中add了method=RequestMethod.GET,若不配置,API 文档会生成 7 种请求方式。

5.API详细说明

    @ApiOperation("信息软删除")
    @ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "操作成功"),
            @ApiResponse(code = CommonStatus.EXCEPTION, message = "服务器内部异常"),
            @ApiResponse(code = CommonStatus.FORBIDDEN, message = "权限不足") })
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "Long", name = "id", value = "信息id", required = true) })
    @RequestMapping(value = "/remove.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RestfulProtocol remove(Long id) {...}

猜你喜欢

转载自blog.csdn.net/zzz127333092/article/details/81513213