java—将tomcat替换为spring boot Key斗篷项目的undertow服务器会导致illegalstateexception:未设置servletcontext

czfnxgou  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(335)

我正在尝试设置一个简单的spring启动示例项目(keydepot教程),但是我遇到了一个启动错误。唯一不同于原始配置的是我想使用undertowserver而不是tomcat。
运行主类而不做任何更改是成功的(用@springbootapplication注解的主类)(mvn spring-boot:run after 清洁和安装)(标准tomcat设置)。
在pom中进行以下更改后,我得到一个错误:
java.lang.illegalstateexception:未设置servletcontext
详见下表:
pom更改:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

而不是

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

使用下拖时Spring启动错误:
12:48:55.077[main]错误o.s.boot.springapplication-应用程序运行失败org.springframework.beans.factory.beancreationexception:创建名为“resourcehandlermapping”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/web/servlet/webmvcautoconfiguration$enablewbmvcconfiguration.class]中定义:bean示例化via工厂方法失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.servlet.handlermapping]:工厂方法'resourcehandlermapping'引发异常;嵌套的异常是java.lang.illegalstateexception:org.springframework.beans.factory.support.constructorresolver.instantiate(constructorresolver)没有设置servletcontext。java:655)在org.springframework.beans.factory.support.constructorresolver.instantialEusingFactoryMethod(constructorresolver。java:635)在org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instanceUsingFactoryMethod(abstractautowirecapablebeanfactory)。java:1336)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory)。java:1176)在org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory)。java:556)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory)。java:516)在org.springframework.beans.factory.support.abstractbeanfactory.lambda$dogetbean$0(abstractbeanfactory)。java:324)位于org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry)。java:226)位于org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.com)。java:322)在org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory)。java:202)位于org.springframework.beans.factory.support.defaultlistablebeanfactory.PreInstanceSingletons(defaultlistablebeanfactory)。java:897)在org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext。java:879)在org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext。java:551)在org.springframework.boot.web.servlet.context.servletwebserverapplicationcontext.refresh(servletwebserverapplicationcontext。java:143)在org.springframework.boot.springapplication.refresh(springapplication。java:758)在org.springframework.boot.springapplication.refresh(springapplication。java:750)在org.springframework.boot.springapplication.refreshcontext(springapplication。java:397)在org.springframework.boot.springapplication.run(springapplication。java:315)在org.springframework.boot.springapplication.run(springapplication。java:1237)在org.springframework.boot.springapplication.run(springapplication。java:1226)在com.baeldung.keydeap.springboot.main(springboot。java:11)原因:org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.servlet.handlermapping]:工厂方法'resourcehandlermapping'引发异常;嵌套的异常是java.lang.illegalstateexception:org.springframework.beans.factory.support.simpleinstationstrategy.instantiate(simpleinstationstrategy)没有设置servletcontext。java:185)在org.springframework.beans.factory.support.constructorresolver.instantiate(constructorresolver。java:650) ... 由于以下原因省略了20个公共帧:java.lang.illegalstateexception:org.springframework.util.assert.state(assert)处未设置servletcontext。java:76)位于org.springframework.web.servlet.config.annotation.webmvcconfigurationsupport.resourcehandlermapping(webmvcconfigurationsupport)。java:534)在java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(本机方法)位于java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)在java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.base/java.lang.reflect.method.invoke(method。java:566) 位于org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy)。java:154) ... 省略21个公共框架
使用的spring-boot.version是2.3.3.release(如父pom中所述)。
我怎样才能让这个工作与一个下拖服务器?

whhtz7ly

whhtz7ly1#

固定的
当如下所示的部分从原始securityconfig类中分离时,它将与undertow一起工作:
(新类)

@Configuration
public class KeycloakConfig {
  @Bean
  public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
    return new KeycloakSpringBootConfigResolver();
  }
}

相关问题