How to configure springdoc-openapi/swagger support for a specific servlet?
Ie. How to register springdoc controllers + swagger webjars under a specific servlet? Ie, don't configure it under the "default" servlet by preventing autoconfigure in root application context?
@SpringBootApplication(exclude = {SpringDocConfiguration.class})
class MyApp {
// ...
}
//---
@Configuration
class RootAppConfiguration {
@Bean
public ServletRegistrationBean webMVCContextPrivate(ApplicationContext parentContext) {
var webApplicationContext1 = new AnnotationConfigServletWebApplicationContext();
webApplicationContext1.setParent(parentContext);
webApplicationContext1.register(PrivateServletConfig.class);
var webApplicationContext = webApplicationContext1;
var dispatcherServlet = new DispatcherServlet(webApplicationContext);
ServletRegistrationBean dispatcher = new ServletRegistrationBean(dispatcherServlet , "/" + REST_API_SERVLET_PATH + "/*");
dispatcher.setName(WEB_MVCCONTEXT_PRIVATE);
dispatcher.setLoadOnStartup(1);
dispatcher.setOrder(Ordered.HIGHEST_PRECEDENCE);
return dispatcher;
}
}
//---
@Configuration
@ComponentScan(basePackages = {
"org.springdoc",
"se.myapp.api.controller",
"se.myapp.api.doc",
"se.myapp.api.domain"
})
public class PrivateServletConfig extends WebMvcConfigurationSupport {
// various config
}
...is what I got now, but it's not behaving well.
I think I might have a similar problem.
SpringDoc is excluded from / Servlet and instead configured in /api Servlet. But SpringDoc seems to only check the context root, not the servlet path, so while manually calling /api/v3/api-docs works fine, but /api/v3/api-docs/swagger-config returns /v3/api-docs instead and thus the UI does not work.
My workaround (actually a colleague found it) was to inject spring.mvc.servlet.path=/api into the configuration of the /api Servlet (via properties file and @PropertySource on a @Configuration in that context)
I also tried setting springdoc.api-docs.path=/api/v3/api-docs, but then /api/v3/api-docs/swagger-config throws a 404 and instead it now only works on /api/api/v3/api-docs/swagger-config (and /api/api/v3/api-docs)... :D
@gurgl,
We use GitHub issues to track bugs and enhancements. If you have a general usage question please ask on Stack Overflow. The springdoc-openapi team and the broader community monitor the springdoc tag.
These are some basic guidelines before opening an issue. First of all you need to make sure, you don’t create duplicate issues, and there no question already answered on https://stackoverflow.com/tags/springdoc.