Мне нужно настроить пользовательский контекст для разных сред, таких как ниже < /p>
dev - dev https://qa.swagger.com/dev/api/myapp/sw ... tmlобразно https://uat.swagger.com/api/myapp/swagg ... разно[code]
org.springframework.boot
spring-boot-starter-parent
2.7.0
org.springdoc
springdoc-openapi-ui
1.6.8
org.springdoc
springdoc-openapi-webmvc-core
1.6.8
[/code]
// 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));
}
}
Код: Выделить всё
spring.application.name=myApp
server.servlet.context-path=/api/${spring.application.name}
@Component
public class SwaggerListener implements ApplicationListener {
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 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;
}
}
< /code>
Добавление выше прослушивателя в основной класс < /p>
@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);
}
}
< /code>
Приведенная выше конфигурация прослушивателя не работает
может кто -нибудь помочь мне здесь и сообщить мне, что мне здесь не хватает? < /p>
Подробнее здесь: https://stackoverflow.com/questions/725 ... -applicati