1. 程式人生 > >生產環境不啟用swagger文件

生產環境不啟用swagger文件

生產環境需要關閉swaggerui

  1. 啟動判斷寫到配置檔案中,根據條件判斷是否載入
@Configuration
@EnableSwagger2
public class SwaggerConfig {

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

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.xx.controller"
)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("RESTful APIs") .description("用於專案前端介面呼叫") .termsOfServiceUrl("") .contact("") .version("1.0"
) .termsOfServiceUrl("11") .build(); } }
  1. 資原始檔是否掃描
@Configuration
@ComponentScan(value = "org.xx.interceptor")
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. 配置檔案中
swagger.show=true