我在Springboot项目中从springfox迁移到OpenApi 3,因为我们需要升级到最新的Springboot版本2.7.0
我需要为不同的环境配置自定义contextPath,如下所示
开发人员-https://qa.swagger.com/dev/api/myApp/swagger-ui/index.html
qa -https://qa.swagger.com/api/myApp/swagger-ui/index.html
网站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类别
@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运行swagger,并从控制器API获取所有需要的响应http://localhost:8082/api/myApp/swagger-ui/index. html #/
为了在其他环境中配置Swaggerurl,我尝试创建一个如下的侦听器配置类,但没有成功
@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;
}
}
正在将上述侦听器添加到主类
@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);
}
}
上面的侦听器配置不起作用,有人能帮我吗?让我知道我在这里错过了什么?
1条答案
按热度按时间uz75evzq1#
我一直看起来一样。此文档帮助我:
要启用对多个OpenAPI定义的支持,需要定义GroupedOpenApi类型的Bean。
对于以下组定义(基于软件包路径),OpenAPI描述URL将为:/v3/api-docs/存储
对于以下组定义(基于软件包名称),OpenAPI描述URL将为:/v3/api-docs/用户
对于以下组定义(基于路径),OpenAPI描述URL将为:/v3/api文档/宠物
对于以下组定义(基于软件包名称和路径),OpenAPI描述URL将为:/v3/api-docs/组
来源:SPRINDOC