如何在spring-boot中完全禁用swagger-ui?(/ swagger-ui.html应返回404) [英] How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)
问题描述
我已阅读以下主题:
看起来不准确。我可以完全禁用此网址吗? 404例如或类似的东西。
我的答案与前面提供的答案类似,但略有不同。我通常会创建一个名为 swagger
的单独弹簧配置文件。当我想启用Swagger时,我在启动应用程序时传递以下VM标志, -Dspring.profiles.active = swagger
。以下是我的Swagger配置示例,
@Profile(value = {swagger})
@Configuration
@EnableSwagger2
公共类SwaggerConfiguration {
...
}
下次当您尝试访问 swagger-ui.html
而不使用 swagger
个人资料时,您将获得一个空的Swagger屏幕,但不是404。
如果您根本不想加载静态Swagger UI页面,您可以编写一个简单的控制器,如下所示,
@配置文件(!swagger)
@RestController
@ Slf4j
公共类DisableSwaggerUiController {
@RequestMapping(value =swagger-ui.html,method = RequestMethod.GET)
public void getSwagger(HttpServletResponse httpResponse)抛出IOException {
httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
}
}
现在,如果您尝试访问 swagger-ui.html
没有 swagger
个人资料,你将获得404.
I have read following topic: Disabling Swagger with Spring MVC
and I wrote:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.project.name.controller"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo())
.enable(false);
}
But in case if I try to access swagger ui: localhost:8080/swagger-ui.html
I see
It looks not accurate. Can I fully disabled this URL ? 404 for example or something like this.
My answer is similar to the answer provided earlier with a slight difference. I usually create a separate spring profile named swagger
. When I want to enable Swagger, l pass the following VM flag while starting my application, -Dspring.profiles.active=swagger
. Here is an example of my Swagger configuration,
@Profile(value = {"swagger"})
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
...
}
Next time when you try to access swagger-ui.html
without swagger
profile, you will get an empty Swagger screen but not 404.
If you don't want to load the static Swagger UI page at all, you can write a simple controller as shown below,
@Profile("!swagger")
@RestController
@Slf4j
public class DisableSwaggerUiController {
@RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
public void getSwagger(HttpServletResponse httpResponse) throws IOException {
httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
}
}
Now if you try to access swagger-ui.html
without swagger
profile, you will get a 404.
这篇关于如何在spring-boot中完全禁用swagger-ui?(/ swagger-ui.html应返回404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!