SpringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:
(1)pom中添加依赖
[java] view plain copy
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
(2)添加Swagger的配置类:
[java] view plain copy
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.XXX.controller")
public class SwaggerConfig{
}
然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/78280051
但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:
[java] view plain copy
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.XXX.controller")
public class SwaggerConfig{
@Autowired
ConfigService configService;
@Bean
public Docket customDocket() {
if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfoOnline())
.select()
.paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合
.build();
}else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX系统")
.description("XXX系统接口")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("","", ""))
.build();
}
private ApiInfo apiInfoOnline() {
return new ApiInfoBuilder()
.title("")
.description("")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("")
.contact(new Contact("","", ""))
.build();
}
}
现在http://localhost/swagger-ui.html这个页面虽然还能访问,那是却看不到任何内容了,包括http://localhost/v2/api-docs也是一样。
应该还有更好的办法!
--------------------------再进一步
修改一下依赖,为了编译通过,单独把swagger-annotations拿出来:
[html] view plain copy
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${springfox-swagger-anno.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>${swagger.scope}</scope>
<version>${springfox-swagger.version}</version>
</dependency>
[html] view plain copy
<profile>
<id>online</id>
<properties>
<profiles.active>online</profiles.active>
<swagger.scope>provided</swagger.scope>
</properties>
</profile>
线上环境把springfox-swagger-ui的依赖配置成是provided,这样可以实现http://localhost/swagger-ui.html访问404。但是:http://localhost/v2/api-docs和http://localhost/swagger-resources等接口依然能够访问,尽管是空的!
(1)pom中添加依赖
[java] view plain copy
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
(2)添加Swagger的配置类:
[java] view plain copy
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.XXX.controller")
public class SwaggerConfig{
}
然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/78280051
但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:
[java] view plain copy
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.XXX.controller")
public class SwaggerConfig{
@Autowired
ConfigService configService;
@Bean
public Docket customDocket() {
if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfoOnline())
.select()
.paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合
.build();
}else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX系统")
.description("XXX系统接口")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("","", ""))
.build();
}
private ApiInfo apiInfoOnline() {
return new ApiInfoBuilder()
.title("")
.description("")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("")
.contact(new Contact("","", ""))
.build();
}
}
现在http://localhost/swagger-ui.html这个页面虽然还能访问,那是却看不到任何内容了,包括http://localhost/v2/api-docs也是一样。
应该还有更好的办法!
--------------------------再进一步
修改一下依赖,为了编译通过,单独把swagger-annotations拿出来:
[html] view plain copy
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${springfox-swagger-anno.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>${swagger.scope}</scope>
<version>${springfox-swagger.version}</version>
</dependency>
[html] view plain copy
<profile>
<id>online</id>
<properties>
<profiles.active>online</profiles.active>
<swagger.scope>provided</swagger.scope>
</properties>
</profile>
线上环境把springfox-swagger-ui的依赖配置成是provided,这样可以实现http://localhost/swagger-ui.html访问404。但是:http://localhost/v2/api-docs和http://localhost/swagger-resources等接口依然能够访问,尽管是空的!