Last active
June 9, 2022 12:32
-
-
Save lbenedetto/cf908164788fc15cda429b8a931c4796 to your computer and use it in GitHub Desktop.
Fix for servlets in SpringDoc to have custom servlet context paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * One of these per servlet | |
| */ | |
| @Component | |
| public class CallbacksApiDocsConfiguration extends CommonApiDocsConfiguration { | |
| public CallbacksApiDocsConfiguration(SwaggerUiConfigProperties swaggerUiConfig, | |
| SpringDocConfigProperties springDocConfigProperties, | |
| SwaggerUiConfigParameters swaggerUiConfigParameters, | |
| SpringWebProvider springWebProvider) { | |
| super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters, springWebProvider); | |
| } | |
| @Override | |
| public OpenAPI config(OpenAPI openAPI) { | |
| return openAPI.info(new Info() | |
| .title("Callbacks API") | |
| .version("1.0.0")); | |
| } | |
| @Override | |
| public String servletContextPath() { | |
| return "callbacks"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springdoc.core.SpringDocConfigProperties; | |
| import org.springdoc.core.SwaggerUiConfigParameters; | |
| import org.springdoc.core.SwaggerUiConfigProperties; | |
| import org.springdoc.core.providers.SpringWebProvider; | |
| import org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc; | |
| import org.springframework.beans.BeansException; | |
| import org.springframework.beans.factory.config.BeanPostProcessor; | |
| import org.springframework.context.annotation.Bean; | |
| import io.swagger.v3.oas.models.OpenAPI; | |
| import lombok.RequiredArgsConstructor; | |
| @RequiredArgsConstructor | |
| public abstract class CommonApiDocsConfiguration implements BeanPostProcessor { | |
| private final SwaggerUiConfigProperties swaggerUiConfig; | |
| private final SpringDocConfigProperties springDocConfigProperties; | |
| private final SwaggerUiConfigParameters swaggerUiConfigParameters; | |
| private final SpringWebProvider springWebProvider; | |
| @Override | |
| public Object postProcessAfterInitialization(Object bean, String beanName) | |
| throws BeansException { | |
| if (SwaggerWelcomeWebMvc.class.isAssignableFrom(bean.getClass())) { | |
| return new FixedSwaggerWelcomeWebMvc( | |
| servletContextPath(), | |
| swaggerUiConfig, | |
| springDocConfigProperties, | |
| swaggerUiConfigParameters, | |
| springWebProvider | |
| ); | |
| } | |
| return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); | |
| } | |
| public abstract String servletContextPath(); | |
| public abstract OpenAPI config(OpenAPI apiConfig); | |
| @Bean | |
| public OpenAPI openApi() { | |
| var apiConfig = new OpenAPI(); | |
| return config(apiConfig); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springdoc.core.SpringDocConfigProperties; | |
| import org.springdoc.core.SwaggerUiConfigParameters; | |
| import org.springdoc.core.SwaggerUiConfigProperties; | |
| import org.springdoc.core.providers.SpringWebProvider; | |
| import org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc; | |
| public class FixedSwaggerWelcomeWebMvc extends SwaggerWelcomeWebMvc { | |
| private final String servletContextPath; | |
| public FixedSwaggerWelcomeWebMvc( | |
| String servletContextPath, | |
| SwaggerUiConfigProperties swaggerUiConfig, | |
| SpringDocConfigProperties springDocConfigProperties, | |
| SwaggerUiConfigParameters swaggerUiConfigParameters, | |
| SpringWebProvider springWebProvider | |
| ) { | |
| super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters, springWebProvider); | |
| this.servletContextPath = "/" + servletContextPath; | |
| } | |
| @Override | |
| protected String buildApiDocUrl() { | |
| var baseUrl = super.buildApiDocUrl(); | |
| if (!baseUrl.startsWith(servletContextPath)) { | |
| baseUrl = servletContextPath + baseUrl; | |
| } | |
| return baseUrl; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment