diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger2Config.java b/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger2Config.java index f3bb9b8f..0cfb9782 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger2Config.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/config/Swagger2Config.java @@ -5,10 +5,13 @@ import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import io.swagger.annotations.ApiOperation; import org.jeecg.common.constant.CommonConstant; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Profile; import org.springframework.util.ReflectionUtils; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; @@ -42,8 +45,13 @@ import java.util.stream.Collectors; @EnableSwagger2 //开启 Swagger2 @EnableKnife4j //开启 knife4j,可以不写 @Import(BeanValidatorPluginsConfiguration.class) +//@Profile({"dev", "local"}) +//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true") public class Swagger2Config implements WebMvcConfigurer { + @Value("${knife4j.production:false}") + private boolean knife4jIsProduction; + /** * * 显示swagger-ui.html文档展示页,还必须注入swagger资源: @@ -52,9 +60,11 @@ public class Swagger2Config implements WebMvcConfigurer { */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); - registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); - registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + if(!knife4jIsProduction){ + registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } } /** @@ -65,6 +75,7 @@ public class Swagger2Config implements WebMvcConfigurer { @Bean(value = "defaultApi2") public Docket defaultApi2() { return new Docket(DocumentationType.SWAGGER_2) + .enable(!knife4jIsProduction) .apiInfo(apiInfo()) .select() //此包路径下的类,才生成接口文档 diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java b/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java index 0e28edc3..a362da7b 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java @@ -52,13 +52,9 @@ public class WebMvcConfiguration implements WebMvcConfigurer { @Value("${spring.resource.static-locations:}") private String staticLocations; - @Value("${knife4j.production}") + @Value("${knife4j.production:false}") private boolean knife4jIsProduction; - - - - @Autowired(required = false) private PrometheusMeterRegistry prometheusMeterRegistry; @@ -85,8 +81,8 @@ public class WebMvcConfiguration implements WebMvcConfigurer { if(!knife4jIsProduction){ registry.addViewController("/").setViewName("doc.html"); } else { - registry.addViewController("/").setStatusCode(HttpStatus.NOT_FOUND); - registry.addViewController("/doc.html").setStatusCode(HttpStatus.NOT_FOUND); + registry.addViewController("/").setViewName("404"); + registry.addViewController("/doc.html").setViewName("404"); } } diff --git a/jeecg-module-system/jeecg-system-start/src/main/resources/application-test.yml b/jeecg-module-system/jeecg-system-start/src/main/resources/application-test.yml index 2ecb181c..95b078dc 100644 --- a/jeecg-module-system/jeecg-system-start/src/main/resources/application-test.yml +++ b/jeecg-module-system/jeecg-system-start/src/main/resources/application-test.yml @@ -246,6 +246,13 @@ logging: level: org.jeecg.modules.system.mapper: info #swagger +springfox: + documentation: + # 总开关(同时设置auto-startup=false,否则/v3/api-docs等接口仍能继续访问) + enabled: false + auto-startup: false + swagger-ui: + enabled: false knife4j: #开启增强配置 enable: true