尝试将tomcat切换为underwow,未显示“ ServletContext set”错误

问题描述

我正在使用spring-boot-starter-web软件包,其中包括Tomcat作为Servlet的认实现。

据我所知,我需要排除spring-boot-starter-tomcat并启用spring-boot-starter-undertow才能使其正常工作。 (不得不说使用Tomcat应用程序运行良好) 但是,尝试通过以下更改运行它时,我遇到了此类错误

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcHandlerMappingIntrospector' defined in class path resource [config/SwaggerConfig.class]: Invocation of init method Failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [config/SwaggerConfig.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcHandlerMappingIntrospector' defined in class path resource [config/SwaggerConfig.class]: Invocation of init method Failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [config/SwaggerConfig.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [config/SwaggerConfig.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set

Caused by: java.lang.IllegalStateException: No ServletContext set

SwaggerConfig.java:

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(regex("/.*"))
                .build();
    }

    @Override
    protected void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/meta-inf/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/meta-inf/resources/webjars/");
    }
}

我已调试它,发现在org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping()内部,如果确实是Tomcat,我确实已经初始化了ServletContext,但是当我拥有Undertow时,该变量为null。 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping()

它不是空值

enter image description here

此处为空

enter image description here

如果您需要其他信息,请告诉我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)