目录
一、Swagger2 依赖
在pom.xml文件中添加如下Swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
二、Swagger2 配置类
SwaggerConfig.java
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;
/**
* Swagger2 配置类
*
* @author: luoyang
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// swagger2核心配置 docket
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
// apiInfo()用于定义api文档汇总信息
.apiInfo(apiInfo())
// select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
.select()
// apis()指定controller包,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)
.apis(RequestHandlerSelectors.basePackage("com.luoy.web"))
// 所有Controller
.paths(PathSelectors.any()).build();
}
// 创建API的基本信息,详见ApiInfoBuilder
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 文档页标题
.title("Spring Boot中使用Swagger2构建RESTful APIs")
// 详细信息
.description("API描述")
// 网站地址
.termsOfServiceUrl("https://swagger.io/")
// 联系人信息
.contact(new Contact("swagger", "https://swagger.io/", "swagger@email.com"))
// 文档版本号
.version("1.0").build();
}
}
三、Swagger2 注解
@Api:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也不看到,所以不需要配置"
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明"
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值
@ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性
四、如何在生产环境关闭Swagger2
方法一:使用@Profile
使用注解@Profile({“dev”,“test”})表示在开发或测试环境开启,而在生产关闭
优点:使用注解的方式,代码更为简洁
缺点:开放swagger的设置,需要涉及到代码的修改
@Configuration
@EnableSwagger2
@Profile({"dev","test"})
public class SwaggerConfig {
// ...
}
方法二:使用@Value(),推荐使用
优点:使用配置文件控制swagger是否开启,修改配置比较灵活
application.yml
spring:
profiles:
# 多环境配置文件激活属性
active: dev
application-dev.yml
#Swagger lock
swagger:
enable: true
Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${swagger.enable}")
private Boolean enable;
/**
* 1.创建Docket,并指定类型
* 2.apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
* 3.select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
* 4.apis()可以指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,
* 并产生文档内容(除了被@ApiIgnore指定的请求)
* @return Docket
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.luoy.web"))
.paths(PathSelectors.any())
.build().enable(enable);
}
/**
* 创建API的基本信息,详见ApiInfoBuilder
* @return ApiInfo
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("API描述")
.termsOfServiceUrl("https://swagger.io/")
.contact(new Contact("swagger", "https://swagger.io/", "swagger@email.com"))
.version("1.0")
.build();
}
}
最后,如果本篇文章对你有帮助,请一键三连支持,谢谢。