怎么样在生产环境中禁用swagger

swagger2.在生产环境中禁用

  1. 启动判断写到配置文件中,根据条件判断是否加载
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger2.enable}")
    private boolean swagger2Enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2Enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.pobo.pbsimu.opcontest.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX-管理端后台服务")
                .description("管理端后台服务")                
                .version("1.0")
                .build();
    }
}
  • 1

  1. 资源文件是否扫描(在springboot环境中这一步不是必须的)
@Configuration
@ComponentScan(value = "com.pobo.pbsimu.opcontest.controller")
class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Value("${swagger.show}")
    private boolean swaggerShow;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (this.swaggerShow) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");

            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
}
  • 1
  1. 2.配置文件中
swagger2.enable=true

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页