<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
新版Maven依赖如下
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
1、Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。
@EnableSwagger2 // Swagger的开关,表示已经启用Swagger
@Configuration // 声明当前配置类 注册到spring容器
public class SwaggerConfiguration {
@Value("${swagger.basePackage}")
private String basePackage; // controller接口所在的包
@Value("${swagger.title}")
private String title; // 当前文档的标题
@Value("${swagger.description}")
private String description; // 当前文档的详细描述
@Value("${swagger.version}")
private String version; // 当前文档的版本
@Bean //配置docket以配置Swagger具体参数
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo2())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage)) // 扫描包
.paths(PathSelectors.any()) // 路径
.build();
}
public ApiInfo apiInfo(){ // ApiInfo这个类没有set方法,只能构造全部属性,全部属性都要写很麻烦
return new ApiInfo("Title",
"Description",
"Version1.0",
"https://91mjw.com/",
new Contact("hujesse","https://www.baidu.com/","hujesse4@163.com"), //作者信息
"Apache 2.0License",
"http://www.apache.org/licenses/LICENSE-2.0 LicenseUrl",
new ArrayList<VendorExtension>());
}
// 可以使用ApiInfoBuilder 来选择实例化
public ApiInfo apiInfo2(){ // 链式编程,看源码吧!
return new ApiInfoBuilder()
.title("Title")
.description("Description")
.build();
}
}
@Value("${swagger.flag}")
private Boolean swaggerFlag;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo2())
.enable(swaggerFlag)
.select()
// 扫描那个包下的注解
.apis(RequestHandlerSelectors.basePackage("com.hujesse.controller"))
.paths(PathSelectors.any())
.build(); //配置了swagger
}
#yaml配置swagger
swagger:
basePackage: com.hujesse.controller
title: Title
description: Des
version: V1.0
flag: true
@Api(value = "hello")
@RestController
public class TestController{
@ApiOperation(value="byebye")
@GetMapping("/hello")
public String hello() {
return "HelloSwa。";
}
}
<!-- 引入swagger-ui-layer包 /docs.html-->
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>1.1.3</version>
</dependency>
链接:https://www.bilibili.com/video/BV1PE411i7CV?p=50&spm_id_from=pageDriver
狂神说java 47 - 50
微信公众号Springboot14:集成swagger终极版
原文:https://www.cnblogs.com/hujesse4/p/14811421.html