如何在生产环境禁用Swagger

SpringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:

(1)pom中添加依赖


			io.springfox
			springfox-swagger-ui
			${springfox-swagger.version}
		
		
			io.springfox
			springfox-swagger2
			${springfox-swagger.version}
		
(2)添加Swagger的配置类:

@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文档呢?试了很多种方式,最终找到一个简单实用的办法:

@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也是一样。

应该还有更好的办法!

参考:http://blog.csdn.net/w4hechuan2009/article/details/68892718

swagger必须要跟springmvc在同一个context才行,springmvc只是spring的一个子context。如果swagger让spring的context加载,那么swagger的那些url用springmvc的拦截器是拦截不到的!

所以,两种解决办法:


如果是使用注解的方式:


(1)spring-mvc的配置:



	
	  
注意要把swagger的配置加进来,同时:


(2)spring的配置:



	
	  
注意把swagger排除掉


(3)Swagger的配置:

@Configuration 
@EnableSwagger2 
@EnableWebMvc
@ComponentScan("com.inspur.eyun.yunbx.controller")
public class SwaggerConfig{
}
注意@Configuration注解。


当然更推荐的办法是使用xml配置的方式,因为这样可以不用引入swagger的依赖包:


(1)spring-mvc的配置:


	
		
	


spring-mvc-swagger.xml:



	SpringMVC Swagger Configuration
	
	

注意:我们这里把swagger单独放到一个配置文件中,如果是线上环境,则文件内容为空,如果是线下测试环境,则配置上Swagger。


(2)spring的配置:


	
		
	


(3)Swagger的配置:

@EnableSwagger2 
@EnableWebMvc
public class SwaggerConfig{

    @Bean
    public Docket customDocket() {
    	return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.inspur.eyun.yunbx.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
    	return new ApiInfoBuilder()
                .title("XXX平台")
                .description("XXX平台接口")
                .license("")
                .licenseUrl("")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .contact(new Contact("","", ""))
                .build();
    }
}

注意:这里我们去掉了@Configuration,同时,修改我们的pom,配置多profile打包:


pom.xml:


		
			io.springfox
			springfox-swagger2
			${springfox-swagger.version}
			${swagger.scope}
		
		
			io.springfox
			springfox-swagger-ui
			${swagger.scope}
			${springfox-swagger-ui.version}
		
注意:这里的依赖的scope是动态设置的,如果是线上环境,我们把scope设置成provided就可以。


		
			dev
			
				dev
				compile
			
			
				true
			
		
		
			test
			
				test
				compile
			
		
		
			online
			
				online
				provided
			
		
	


通过不同的profile给swagger的依赖设置不同的scope!


注意:springfox-swagger.version=2.7.0有bug,可以使用低版本2.6.1。太他妈的坑!


完美!


你可能感兴趣的:(java)