springBoot 整合swagger
1、pom.xml 配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
2、swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //这里采用包含注解的方式来确定要显示的接口
.paths(PathSelectors.any())
.build();
} @SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口说明文档")
.description("app doc")
//.termsOfServiceUrl("http://xxxx")
.contact("")
.version("1.0")
.build();
} }
3、Application类
public class AppApplication extends WebMvcConfigurerAdapter { @Value("${spring.swaggerOpenFlag}")
private boolean swaggerOpenFlag; /**
* 增加静态资源
* */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(swaggerOpenFlag){
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
} }
4、application.yml
spring:
profiles: dev
swaggerOpenFlag: true #是否开启swagger spring:
profiles: produce
swaggerOpenFlag: false #是否开启swagger
5、访问swagger
访问地址 http://localhost:8888/swagger-ui.html