如何在OpenAPI3 Springboot应用程序中拥有Swagger URL的自定义上下文路径
我在SpringFoot项目中从Springfox迁移到OpenAPI3,因为我们需要使用2.7.0版升级到最新的Springboot,
配置自定义上下文路径
我需要为Dev -dev -href =“ https:// qa) 。 swagger.com/dev/api/myapp/swagger-ui/index.html“ rel =“ nofollow noreferrer”> https://qa.swagger.com/dev/dev/api/myapp/myapp/swagper/swagper/swagger/swagger-ui/index.html
qa- https://qa.swagger.com/api/api/api/ myApp/swagger-ui/index.html
uat- https://uat.swagger.com/api/myapp/swagper/swagger/index.html#/
// pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>1.6.8</version>
</dependency>
// swaggerconfig class
@Configuration
@Profile({ "local", "dev", "qat", "uat" })
public class SwaggerConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI().info(info());
}
private Info info() {
return new Info()
.title(title)
.version(version)
.license(new License().name(licenseName).url(licenseUrl));
}
}
//application.properties,
spring.application.name=myApp
server.servlet.context-path=/api/${spring.application.name}
带有上述配置,我能够使用以下URL进行招摇,并从控制器API中获取所有必需的响应 http:// localhost:8082/api/myapp/swagger-ui/index.html#/
如下
@Component
public class SwaggerListener implements ApplicationListener<ApplicationPreparedEvent> {
final ApplicationPreparedEvent event = null;
@Override
public void onApplicationEvent(final ApplicationPreparedEvent event) {
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
Properties properties = new Properties();
properties.put("springdoc.swagger-ui.path", swaggerPath(event));
environment.getPropertySources().addFirst(new PropertiesPropertySource("programmatically", properties));
}
private String swaggerPath(final ApplicationPreparedEvent event) {
String basePath = null;
String swagger = "swagger-ui/index.html";
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
String[] profilesList = environment.getActiveProfiles();
List<String> profiles = Arrays.asList(profilesList);
String contextPath = environment.getProperty("server.servlet.context-path");
if (profiles != null && (profiles.contains("local"))) {
basePath = swagger;
} else if (profiles != null && profiles.contains("dev")) {
basePath = "/dev/api/myApp/" + swagger;
} else if (profiles != null && (profiles.contains("qat") || profiles.contains("uat"))) {
basePath = "/api/myApp/";
}
return basePath;
}
}
用于为其他环境配置Swagger URL,我尝试创建一个侦听器配置类, 类
@SpringBootApplication(scanBasePackages = { "com.myApp.controller" })
@OpenAPIDefinition
public class myApi {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(myApi.class);
springApplication.addListeners(new SwaggerListener());
springApplication.run(args);
}
}
上述侦听器配置不起作用 谁能在这里帮助我,让我知道我在这里想念什么?
I am migrating from springfox to OpenApi3 in my Springboot project as we have a requirement to upgrade to latest springboot with version 2.7.0
I need to configure custom contextPath for different environments like below
dev - https://qa.swagger.com/dev/api/myApp/swagger-ui/index.html
qa - https://qa.swagger.com/api/myApp/swagger-ui/index.html
uat - https://uat.swagger.com/api/myApp/swagger-ui/index.html#/
// pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>1.6.8</version>
</dependency>
// SwaggerConfig class
@Configuration
@Profile({ "local", "dev", "qat", "uat" })
public class SwaggerConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI().info(info());
}
private Info info() {
return new Info()
.title(title)
.version(version)
.license(new License().name(licenseName).url(licenseUrl));
}
}
//application.properties
spring.application.name=myApp
server.servlet.context-path=/api/${spring.application.name}
With the above configuration, I am able to run swagger using below url and getting all required response from controller apis
http://localhost:8082/api/myApp/swagger-ui/index.html#/
For configuring the Swagger url for other environments,I tried to create a listener configuration class like below which didn't work
@Component
public class SwaggerListener implements ApplicationListener<ApplicationPreparedEvent> {
final ApplicationPreparedEvent event = null;
@Override
public void onApplicationEvent(final ApplicationPreparedEvent event) {
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
Properties properties = new Properties();
properties.put("springdoc.swagger-ui.path", swaggerPath(event));
environment.getPropertySources().addFirst(new PropertiesPropertySource("programmatically", properties));
}
private String swaggerPath(final ApplicationPreparedEvent event) {
String basePath = null;
String swagger = "swagger-ui/index.html";
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
String[] profilesList = environment.getActiveProfiles();
List<String> profiles = Arrays.asList(profilesList);
String contextPath = environment.getProperty("server.servlet.context-path");
if (profiles != null && (profiles.contains("local"))) {
basePath = swagger;
} else if (profiles != null && profiles.contains("dev")) {
basePath = "/dev/api/myApp/" + swagger;
} else if (profiles != null && (profiles.contains("qat") || profiles.contains("uat"))) {
basePath = "/api/myApp/";
}
return basePath;
}
}
Adding above listener to main class
@SpringBootApplication(scanBasePackages = { "com.myApp.controller" })
@OpenAPIDefinition
public class myApi {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(myApi.class);
springApplication.addListeners(new SwaggerListener());
springApplication.run(args);
}
}
The above listener configuration is not working
Can anyone help me out here and let me know what am I missing here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直在看一样。该文档对我有帮助:
来源: sprindoc
I have been looking the same. This documentation help me:
Source: SPRINDOC