[英]How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)
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
但是,如果我尝试访问 swagger ui:
localhost:8080/swagger-ui.html
I see我懂了
It looks not accurate.看起来不准确。 Can I fully disabled this URL ?
我可以完全禁用这个 URL 吗? 404 for example or something like this.
例如 404 或类似的东西。
My answer is similar to the answer provided earlier with a slight difference.我的答案与之前提供的答案相似,但略有不同。 I usually create a separate spring profile named
swagger
.我通常创建一个名为
swagger
的单独弹簧配置文件。 When I want to enable Swagger, l pass the following VM flag while starting my application, -Dspring.profiles.active=swagger
.当我想启用 Swagger 时,我会在启动我的应用程序时传递以下 VM 标志
-Dspring.profiles.active=swagger
。 Here is an example of my Swagger configuration,这是我的 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
配置文件的情况下访问swagger-ui.html
,您将看到一个空的 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
配置文件的情况下访问swagger-ui.html
,您将得到 404。
with swagger 3.0.0 version you can add springfox.documentation.enabled=false
in corresponding environment profile application.properties
file.使用 swagger 3.0.0 版本,您可以在相应的环境配置文件
application.properties
文件中添加springfox.documentation.enabled=false
。 For example, I have added this to application-prod.properties
to disable in production (while running the app you must specify the profile using VM args like -Dspring.profiles.active=prod
)例如,我已将此添加到
application-prod.properties
以在生产中禁用(在运行应用程序时,您必须使用 VM args 指定配置文件,如-Dspring.profiles.active=prod
)
You can externalize the @EnableSwagger2
to its own @Configruation
and load it conditionally via a property or profile.您可以将
@EnableSwagger2
外部@EnableSwagger2
它自己的@Configruation
并通过属性或配置文件有条件地加载它。 eg例如
@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
//Additional Swagger Beans
} }
this would activate swagger for any profile that isn't production.这将为任何非生产配置文件激活招摇。
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>
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 构建中被覆盖
Just remove dependency.只需删除依赖项。
<!--<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>-->
It does not affect compiling.它不影响编译。
For SpringDoc users, add this to your application.properties
对于 SpringDoc 用户,将此添加到您的
application.properties
springdoc.api-docs.enabled=false
To disable Swagger only when the prod
profile is active, add it to your application-prod.properties
instead要仅在
prod
配置文件处于活动状态时禁用 Swagger,请将其添加到您的application-prod.properties
Adding onto @Hayden's answer (I don't have enough points to comment..)添加到@Hayden 的答案(我没有足够的点数来评论..)
According to the springdoc documentation, you can disable both the springdoc api endpoints and swagger-ui using the following properties:根据 springdoc 文档,您可以使用以下属性禁用 springdoc api 端点和 swagger-ui:
https://springdoc.org/#disabling-the-springdoc-openapi-endpoints https://springdoc.org/#disabling-the-springdoc-openapi-endpoints
# Disabling the /v3/api-docs enpoint
springdoc.api-docs.enabled=false
https://springdoc.org/#disabling-the-swagger-ui https://springdoc.org/#disabling-the-swagger-ui
# Disabling the swagger-ui
springdoc.swagger-ui.enabled=false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.