【问题标题】:Spring Swagger2 integration ServletContext autowiring issueSpring Swagger2 集成 ServletContext 自动装配问题
【发布时间】:2017-06-15 12:32:02
【问题描述】:

我正在运行带有嵌入式 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 as next

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

那么任何人都可以提出一些解决这个自动装配问题的方法吗?

【问题讨论】:

  • 你在使用 Spring Boot 吗?如果是,请将 Bean 注释更改为引导程序类上的 Configuration。
  • @duffymo,我正在使用 Spring MVC
  • Spring 还是 Spring Boot?
  • @duffymo,好的)我没有使用 Spring boot
  • 什么是空? NPE 扔到哪里去了?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这是你的问题:

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

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

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

    【讨论】:

    • 可能我错过了配置的网络应用程序,现在我无法访问 ServletContext
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2011-12-29
    • 2015-11-22
    • 2011-06-19
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多