Gateway网关报错Unable to start ServletWebServerApplicationContext due to missing ServletWebServer

问题报错:

报错1:

Gateway网关报错Unable to start ServletWebServerApplicationContext due to missing ServletWebServer_第1张图片

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'conversionServicePostProcessor' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Cannot register bean definition [Root bean: class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=conversionServicePostProcessor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]] for bean 'conversionServicePostProcessor': There is already [Root bean: class [org.springframework.security.config.annotation.web.reactive.WebFluxSecurityConfiguration]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=conversionServicePostProcessor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/security/config/annotation/web/reactive/WebFluxSecurityConfiguration.class]] bound.
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:945)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:286)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:144)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)

报错2:

Gateway网关报错Unable to start ServletWebServerApplicationContext due to missing ServletWebServer_第2张图片

org.springframework.http.codec.ServerCodecConfigurer’ that could not be found

问题分析:

这个报错的意思就是说没有找到ServeltWebServer这个bean,而很明显这个bean是关于web的。因此我们就得从这个方向入手。而会出现找不这个bean的,要么就是没有配置上web服务,要么就是没有配置上这个bean,又或者是发生了冲突

问题解决:

普通服务:

如果是普通项目的话,基本就是缺少web这个服务。

解决方案1:

pom文件配置


            org.springframework.boot
            spring-boot-starter-web
        

解决方案2:

看你启动类这写入的类是否是一致的

Gateway网关报错Unable to start ServletWebServerApplicationContext due to missing ServletWebServer_第3张图片

解决方案3:

是否配置了@SpringBootApplication

Gateway网关服务:

解决方案1: 

而对于spring cloud gateway来说是基于webflux而不是web框架。因此导入pom需要导入


            org.springframework.boot
            spring-boot-starter-webflux
        

如果有web的则需要删除该pom坐标。或者引入其他项目含有该pom文件的的时候需要去除该坐标

Gateway网关报错Unable to start ServletWebServerApplicationContext due to missing ServletWebServer_第4张图片

解决方案2(博主认为最有用的):

实际上以上的方案我都试过,但就是行不通。导入这个webflux后还是会报其他奇怪的错误,通过exclusions关键字来进行去除之后又会报其他错误。这就证明并非这样解决的。

因此直接在配置文件中配置以下即可

spring:
  main:
    web-application-type: reactive

原理: 

当设置为reactive时,Spring Boot将使用反应式编程模型来构建应用。反应式编程是一种异步编程范式,关注数据流和变化传播。这意味着它可以更好地处理非阻塞I/O,从而提高应用的可伸缩性和响应性。 这种设置通常与Spring WebFlux一起使用,Spring WebFlux是Spring 5.0引入的一个新的反应式Web框架。它可以与传统的Servlet API(通过web-application-type: servlet设置)并存,或者作为一个独立的反应式堆栈运行。 总的来说,web-application-type: reactive的作用是让Spring Boot应用使用反应式编程模型,以提高应用的性能和可伸缩性。

 
    

你可能感兴趣的:(项目,杂谈,spring,boot,spring,cloud,spring,gateway)