- 启动判断写到配置文件中,根据条件判断是否加载
@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.资源文件是否扫描(在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/"); } } }
- 2.配置文件中
swagger2.enable=true
参考:https://blog.csdn.net/qq_40006446/article/details/80322864