java 如何在 spring-boot 中完全禁用 swagger-ui?(/swagger-ui.html 应该返回 404)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46454473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)
提问by gstackoverflow
I have read following topic: Disabling Swagger with Spring MVC
我已阅读以下主题: 使用 Spring MVC 禁用 Swagger
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
但是,如果我尝试访问 swagger ui:localhost:8080/swagger-ui.html
我明白了
It looks not accurate. Can I fully disabled this URL ? 404 for example or something like this.
看起来不准确。我可以完全禁用这个 URL 吗?例如 404 或类似的东西。
回答by Indra Basak
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,
我的答案与之前提供的答案相似,但略有不同。我通常创建一个名为swagger
. 当我想启用 Swagger 时,我会在启动我的应用程序时传递以下 VM 标志-Dspring.profiles.active=swagger
. 这是我的 Swagger 配置的示例,
@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.
下次当您尝试在swagger-ui.html
没有swagger
配置文件的情况下访问时,您将看到一个空的 Swagger 屏幕,但不会出现 404。
If you don't want to load the static Swagger UI page at all, you can write a simple controller as shown below,
如果你根本不想加载静态 Swagger UI 页面,你可以编写一个简单的控制器,如下所示,
@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.
现在,如果您尝试在swagger-ui.html
没有swagger
配置文件的情况下访问,您将收到 404。
回答by Darren Forsythe
You can externalize the @EnableSwagger2
to its own @Configruation
and load it conditionally via a property or profile. e.g.
您可以@EnableSwagger2
将其外部化,@Configruation
并通过属性或配置文件有条件地加载它。例如
@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
//Additional Swagger Beans
}
}
this would activate swagger for any profile that isn't production.
这将为任何非生产配置文件激活招摇。
回答by Ramon Franquesa
If you dont have Swagger annotations inside controllers... just exclude SwaggerConfig.class and swagger dependencies on build
如果您在控制器中没有 Swagger 注释......只需排除 SwaggerConfig.class 和 swagger 对构建的依赖
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/company/app/SwaggerConfig.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</exclude>
<exclude>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by Jacques Koorts
For those that use the code gen:
对于那些使用代码生成器的人:
@Controller @Profile({"dev", "staging"}) public class HomeController { @RequestMapping(value = "/") public String index() { System.out.println("swagger-ui.html"); return "redirect:swagger-ui.html"; } }
And add the file to you .swagger-codegen-ignore else your changes are overwritten on the next maven build
并将文件添加到您 .swagger-codegen-ignore 否则您的更改将在下一个 maven 构建中被覆盖