Spring Boot 禁用 Swagger
时间: 2023-10-20 16:07:16 浏览: 319
在 Spring Boot 项目中,可以通过添加配置来禁用 Swagger。
1. 针对整个应用禁用 Swagger
可以在应用的 `application.properties` 文件中添加以下配置:
```properties
springfox.documentation.enabled=false
```
或者在应用的启动类上添加 `@EnableSwagger2` 注解,并重写 `addResourceHandlers` 方法,将 Swagger UI 的访问路径重定向到 `/error`:
```java
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/")
.setCachePeriod(0);
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(0);
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(0);
registry.addResourceHandler("/swagger-resources/**")
.addResourceLocations("classpath:/META-INF/swagger-resources/")
.setCachePeriod(0);
}
}
```
2. 针对指定的 API 禁用 Swagger
可以在 API 的实现类上添加 `@ApiIgnore` 注解,例如:
```java
@RestController
@RequestMapping("/api")
@ApiIgnore
public class MyController {
// ...
}
```
这样,Swagger 将不会显示该 API。
阅读全文
相关推荐
















