Spring Boot版本:2.4.5
1.引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2. Swagger 配置
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
@Value("${swagger.enable}")
private boolean enableSwagger;
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("zzyNotes")
.description("zzyNotes 这里是描述")
.termsOfServiceUrl("声明:仅作为学习使用")
.contact(new Contact("zzy", "www.baidu.com", "zzy530783806@163.com"))
.version("1.0")
.build();
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("我是swagger")
.enable(true)
.select()
.apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.basePackage("com.zzy.zzyNotes.controller.study"))
.apis(Predicates.or(
RequestHandlerSelectors.basePackage("com.ftsino.citychallenge.api.controller.arScenic"),
RequestHandlerSelectors.basePackage("com.ftsino.citychallenge.api.controller.scenic")
))
.paths(PathSelectors.any())
.paths(PathSelectors.none())
.paths(PathSelectors.ant("/**"))
.paths(Predicates.or(
PathSelectors.ant("/newSysBoxActivities/**"),
PathSelectors.ant("/newSysBoxInfo/**")))
.paths(PathSelectors.regex("正则表达式"))
.build()
.globalOperationParameters(globalOperation());
}
private List<Parameter> globalOperation(){
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return pars;
}
}
3.正式环境不展示
@Value("${swagger.enable}")
private boolean enableSwagger;
方法1: 创建 Docket 时的.enable()
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("我是swagger")
.enable(enableSwagger)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.globalOperationParameters(globalOperation());
}
注意!:enable(false)时swagger也会加载,只是这个docket不展示
方法2: 类上加@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 建议使用
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
}
注意!:关键是@ConditionalOnPropert,它有两个参数,name和havingValue
name加载配置文件,如果这个值和havingValue的值相同,则spring将加载这个Bean,反之不加载
4.常用注解
1.@Api()
--加在controller上
--参数
tags:列表controller名字
2.@ApiOperation()
--加在接口方法上
--参数
value=“方法的用途和作用”
notes=“方法的注意事项和备注”
tags:说明该方法的作用,参数是个数组,可以填多个。
格式:tags={“作用1”,“作用2”}
(在这里建议不使用这个参数,会使界面看上去有点乱,前两个常用)
response:返回类.class
3.@ApiModel()
--加载实体类上
--参数
value=“名字”
description=“描述实体的作用”
4.@ApiModelProperty()
--用在实体字段上
--参数
value=“用户名” 描述参数的意义
name=“name” 参数的变量名
required=true 参数是否必选
5.@ApiImplicitParams
-- 用在接口方法上
--参数
@ApiImplicitParam
6.@ApiImplicitParam
-- 用在接口方法上
--
name=“参数ming”
value=“参数说明”
dataType=“数据类型”
paramType=“query” 表示参数放在哪里
-header–>请求参数的获取:@RequestHeader(代码中接收注解)
-query–>请求参数的获取:@RequestParam(代码中接收注解)
-path(用于restful接口)–>请求参数的获取:@PathVariable(代码中接收注解)
-body–>请求参数的获取:@RequestBody(代码中接收注解)
-form(不常用,form.serilize())
defaultValue=“参数的默认值”
required=“true” 表示参数是否必须传
7.@ApiResponses()
-- 用在接口方法上
--参数
@ApiResponse
8.@ApiResponse()
-- 用在接口方法上
--参数
code = 返回码
message = 对应消息