官网:https://swagger.io/
在项目中使用Swagger需要springfox;
SpringBoot集成Swagger
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
http://localhost:8080/swagger-ui.html
Springboot版本与springfox版本可能存在版本冲突问题
解决办法:https://blog.csdn.net/hadues/article/details/123753888
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
//配置Swagegr信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("张楷涛","https://blog.csdn.net/Littewood?type=blog","[email protected]");
return new ApiInfo(
"张楷涛的SwaggerAPI文档",
"萤火之光,褶褶生辉",
"1.0",
"https://blog.csdn.net/Littewood?type=blog",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
Docker.select()
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//.enable(false) //是否启用swagger,如果为false,则swagger不能再浏览器中访问
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
.apis(RequestHandlerSelectors.basePackage("com.zkt.controller"))
//basePackage():指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象,即如果类上游这个注解就会被扫描
//withMethodAnnotation:扫描方法上的注解
//------------------------------------
//paths():过滤路径
// .paths(PathSelectors.ant("/zkt/**"))
.build()
;
}
如果要在开发环境下显示swagger而在生产环境下关闭swagger要怎么实现
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){
//获取项目环境
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag) //是否启用swagger,如果为false,则swagger不能再浏览器中访问
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
.apis(RequestHandlerSelectors.basePackage("com.zkt.controller"))
//basePackage():指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象,即如果类上游这个注解就会被扫描
//withMethodAnnotation:扫描方法上的注解
//------------------------------------
//paths():过滤路径
// .paths(PathSelectors.ant("/zkt/**"))
.build()
;
}
//配置Swagegr信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("张楷涛","https://blog.csdn.net/Littewood?type=blog","892640297@");
return new ApiInfo(
"张楷涛的SwaggerAPI文档",
"萤火之光,褶褶生辉",
"1.0",
"https://blog.csdn.net/Littewood?type=blog",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
.groupName()
如果需要配置多个分组,则新建Bean
@Bean
public Docket docket1() {
return new Docket(DocumentationType.SWAGGER_2).groupName("张三");
}
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2).groupName("李四");
}
#为实体类添加注释,在Swagger中显示
#为实体类添加注释,在Swagger中显示
@ApiModel("用户实体类")
@ApiModelProperty(“用户名”):为实体类中的属性添加注释,在Swagger中显示
用在属性上,描述响应类的属性
value–字段说明;name–重写属性名字;dataType–重写属性类型;required–是否必填;example–举例说明;hidden–隐藏
@ApiModelProperty(value = “请求返回code说明,0-成功,1-失败”,required = true,example = “0”)
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
如果实体类属性被private修饰,需要有get和set方法
@Api用在请求的类上,表示对类的说明(Controller层)
tags=“说明该类的作用,可以在UI界面上看到的注解”
value=“该参数没什么意义,在UI界面上也看到,所以不需要配置”
@ApiOperation 用在请求的方法上,说明方法的用途、作用
@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response =“接口返回参数类型”, notes = “接口发布说明”;
@ApiParam用于接口处表明需要的参数声明
【注意】在正式发布的时候,关闭Swagger!处于安全考虑,而且节省运行的内存。