Reputation: 2029
I'm running Spring 4 mvc with embedded Jetty 9. I tried to plug in Swagger2 tool, but I faced next exception
Error creating bean with name 'documentationPluginsBootstrapper'
the root cause of this exception is
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency [javax.servlet.ServletContext]: expected at least 1 bean which qualifies as autowire candidate for this dependency.
This is my SwaggerConfigClass
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private static final Logger logger = Logger.getLogger(SwaggerConfig.class);
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("TITLE")
.description("DESCRIPTION")
.version("VERSION")
.termsOfServiceUrl("http://terms-of-services.url")
.license("LICENSE")
.licenseUrl("http://url-to-license.com")
.build();
}
}
After that I created factory class
public class ServletContextFactory implements FactoryBean<ServletContext>,
ServletContextAware {
private ServletContext servletContext;
@Override
public ServletContext getObject() throws Exception {
return servletContext;
}
@Override
public Class<?> getObjectType() {
return ServletContext.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
and then manually used it in redeclared bean
@Bean
public DocumentationPluginsBootstrapper documentationPluginsBootstrapper(DocumentationPluginsManager documentationPluginsManager,
List<RequestHandlerProvider> handlerProviders,
DocumentationCache scanned,
ApiDocumentationScanner resourceListing,
TypeResolver typeResolver,
Defaults defaults) throws Exception {
return new DocumentationPluginsBootstrapper(documentationPluginsManager,
handlerProviders,
scanned,
resourceListing,
typeResolver,
defaults,
new ServletContextFactory().getObject());
}
but still had an exception (another exception) NullPointerException as next
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
So can anyone suggest some solution of this autowiring problem?
Upvotes: 0
Views: 2840
Reputation: 2029
So, the reason why Swagger did not start was refreshing WebContext of my application manually before jetty server start. As a result ServletContext Bean was not added into the WebContext at the time of Swagger initialization.
Upvotes: 1
Reputation: 308763
Here's your problem:
return new DocumentationPluginsBootstrapper(documentationPluginsManager,
handlerProviders,
scanned,
resourceListing,
typeResolver,
defaults,
new ServletContextFactory().getObject());
Spring is out of the picture the moment you call new
to create your ServletContextFactory
. It's your responsibility to inject the dependencies into ServletContextFactory
.
This is a common mistake by new Spring users.
Upvotes: 0