Swagger2
一、整合Swagger2
1.1、在pom.xml中添加依赖
<!-- swagger2 -->
<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>
1.2、编写swagger2配置类
@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://localhost:8090/swagger-ui.html")
.contact(new Contact("qixin","","330553352@qq.com"))
.version("1.0")
.build();
}
}
1.3、添加文件内容
@Api(value = "swagger2", description = "swagger2", position = 100, protocols = "http")
@Resource(name = "swagger2")
public class WgsjzlqsyController {
@GetMapping("/list")
@ApiOperation(value="swagger2", notes="swagger2",httpMethod = "GET")
public List<Wgsjzlqsy> list(){
return service.findList();
}
}
二、生产环境下禁用Swagger2
2.1、方式一
在Swagger2配置类中指定环境:@Profile({“dev”,“test”})
2.2、方式二
- 在配置文件中(application.properties)设置开关:
#swagger开关
swagger.enable=true - 在Swagger2配置类中使用Swagger2开关功能:
@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)