Spring Swagger2 集成 ServletContext 自动装配问题

Spring Swagger2 integration ServletContext autowiring issue

我是 运行 Spring 4 mvc,带有嵌入式 Jetty 9。 我尝试插入 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();
    }
}

之后我创建了工厂 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;
    }

}

然后在重新声明的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 as next

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

那么谁能提出一些解决这个自动装配问题的方法?

这是你的问题:

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

Spring 在您调用 new 创建您的 ServletContextFactory 的那一刻不在画面中。将依赖项注入 ServletContextFactory 你的 责任。

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

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