SpringBoot 集成 Swagger2

1、引入包

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

2、告诉spring boot启用swagger

@Configuration
@EnableSwagger2
public class Swagger2Configuration {
   @Value("${xxx.enabled}") // true false 放在配制文件中,以便可以启用或者停用swagger
 enablexx
//api接口包扫描路径 
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.xx.xx"; //您所在的application的包名。 
public static final String VERSION = "1.0.0"; 
@Bean 
public Docket createRestApi() { 
return new Docket(DocumentationType.SWAGGER_2).enable(enablexx)
                       .apiInfo(apiInfo())
                       .select().apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) 
                       .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                       .build();
       }
       
       private ApiInfo apiInfo() {
           return new ApiInfoBuilder()
                       .title("xxxxx") //设置文档的标题
                       .description("xxxxx API 接口文档") // 设置文档的描述
                       .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                       .termsOfServiceUrl("http://www.xxx.com") // 设置文档的License信息->1.3 License information
                       .build();
       }
    
}

3、访问 http://localhost:port/swagger-ui.html

port:就是您访问服务器的端口,启动完以后,看console就会显示。

4、文档中会显示controllers与models

当然您也可以加入一些注解说明,以便组成员能够看得懂。

4.1  

@Api(tags = "类说明")  ==> description已过时 ==> tags
@RestController 
@RequestMapping(
"/xxxx")

public class xxClassname {
     ...
}

description说明是写在类后面
现在
说明中文 类说明英文
 
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:描述一个请求参数,可以配置参数的中文含义,还可以给参数设置默认值
@ApiImplicitParams:描述由多个 @ApiImplicitParam 注解的参数组成的请求参数列表

参考:https://www.jianshu.com/p/c79f6a14f6c9  (比较详细)

道法自然
原文地址:https://www.cnblogs.com/jiduoduo/p/14918765.html