Spring Swagger2集成ServletContext自动装配问题

时间:2017-06-15 12:32:03

标签: java spring spring-mvc swagger swagger-2.0

我正在使用嵌入式Jetty 9运行Spring 4 mvc。 我试图插入Swagger2工具,但我遇到了下一个异常

Error creating bean with name 'documentationPluginsBootstrapper'

此异常的根本原因是

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.

这是我的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();
    }
}

之后我创建了工厂类

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;
    }

}

然后在重新声明的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());
}

但仍然有一个异常(另一个例外)NullPointerException作为下一个

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

那么有人可以建议解决这个自动装配问题吗?

2 个答案:

答案 0 :(得分:1)

这是你的问题:

return new DocumentationPluginsBootstrapper(documentationPluginsManager,
    handlerProviders,
    scanned,
    resourceListing,
    typeResolver,
    defaults,
    new ServletContextFactory().getObject());

当您致电new创建ServletContextFactory时,春天就不在了。将依赖项注入ServletContextFactory 的责任。

这是新Spring用户常犯的错误。

答案 1 :(得分:0)

因此,在Jetty服务器启动之前,Swagger没有启动的原因是刷新我的应用程序手动的WebContext 。因此,在Swagger初始化时, ServletContext Bean未添加到WebContext 中。