Spring boot配置 swagger
1.导入maven依赖
<!--配置swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--swagger第三方ui-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.swagger配置类
@EnableSwagger2 // Swagger的开关,表示已经启用Swagger
@Configuration // 声明当前配置类
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
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.version(version)
.build();
}
}
3.application.yml
# 配置swagger
swagger:
basePackage: com.xx.demo.controller #包名
title: 标题 #标题
description: 项目文档 #描述
version: V1.0 #版本号
4.在controller里使用
@Api(tags = {"测试类"})
@RestController
@RequestMapping("/test")
public class TestController {
@ApiOperation(value = "测试方法")
@GetMapping("/xrx")
public String xrx() {
return "hello";
}
}
5.访问swagger
http://localhost:8080/swagger-ui.html
http://localhost:8080/doc.html