Springfox

在 Spring Boot 中自定义 Swagger URL

1、概览 Springfox 和 SpringDoc 这两个工具简化了 Swagger API 文档的生成和维护。 在本教程中,我们将了解如何在 Spring Boot 应用中更改 Swagger-UI URL 前缀。 2、使用 Springdoc 时更改 Swagger UI URL 前缀 首先,我们可以看看 如何使用 OpenAPI 3.0 生成 REST API 文档。 根据上述教程,我们需要添加如下 SpringDoc 的依赖: <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.2</version> </dependency> swagger-ui 的默认 URL 是 http://localhost:8080/swagger-ui.html。 比方说,我们要增加 /myproject 前缀,接下来让我们看看自定义 swagger-UI URL 的两种方法。 2.1、application.properties 我们可以在 application.properties 文件中添加以下属性来修改 swagger-UI URL: springdoc.swagger-ui.disable-swagger-default-url=true springdoc.swagger-ui.path=/myproject 2.2、配置类 我们也可以通过配置类来实现: @Component public class SwaggerConfiguration implements ApplicationListener<ApplicationPreparedEvent> { @Override public void onApplicationEvent(final ApplicationPreparedEvent event) { ConfigurableEnvironment environment = event.