现象
spring gateway 遇到异常,无法启动,异常如下:
14:07:09.901 [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at cn.devmgr.mall.gateway.GatewayMpApplication.main(GatewayMpApplication.java:20)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 8 common frames omitted
原因
出现这种状况,是由于项目中混入了 spring-webmvc 的包导致的,spring gateway 使用 webflux, 而webflux和webmvc不兼容,就会出现上面的错误。
解决方法
一般不是直接包含了 spring-webmvc,是引入的某个包需要依赖webmvc,找到并排除就好了,但找起来,需要点方法。
用命令 mvn -X package
增加一个 -X 参数去打包,会把详细的依赖路径全部都显示出来。例如:
[INFO] <<< spring-boot-maven-plugin:2.2.1.RELEASE:run (default-cli) < test-compile @ gateway-mp-server <<<
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=1, ConflictMarker.markTime=0, ConflictMarker.nodeCount=909, ConflictIdSorter.graphTime=1, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=230, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=15, ConflictResolver.conflictItemCount=552, DefaultDependencyCollector.collectTime=27, DefaultDependencyCollector.transformTime=17}
[DEBUG] cn.devmgr.mall:gateway-mp-server:jar:1.0.0
[DEBUG] cn.devmgr:common-api:jar:1.0.0:compile
[DEBUG] org.springframework:spring-webmvc:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework:spring-beans:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework:spring-context:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework:spring-expression:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework:spring-web:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework.security:spring-security-web:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework.security:spring-security-core:jar:5.2.1.RELEASE:compile
[DEBUG] com.auth0:java-jwt:jar:3.9.0:compile
[DEBUG] com.fasterxml.jackson.core:jackson-databind:jar:2.10.0:compile (version managed from 2.10.0.pr3 by com.fasterxml.jackson:jackson-bom:2.10.0)
[DEBUG] commons-codec:commons-codec:jar:1.13:compile (version managed from 1.12 by org.springframework.boot:spring-boot-dependencies:2.2.1.RELEASE)
[DEBUG] org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:jar:2.2.0.RELEASE:compile
[DEBUG] org.springframework.cloud:spring-cloud-starter:jar:2.2.0.RELEASE:compile
[DEBUG] org.springframework.cloud:spring-cloud-context:jar:2.2.0.RELEASE:compile
[DEBUG] org.springframework.security:spring-security-crypto:jar:5.2.1.RELEASE:compile
[DEBUG] org.springframework.cloud:spring-cloud-commons:jar:2.2.0.RELEASE:compile
[DEBUG] org.springframework.security:spring-security-rsa:jar:1.0.7.RELEASE:compile
[DEBUG] org.bouncycastle:bcpkix-jdk15on:jar:1.60:compile
[DEBUG] org.bouncycastle:bcprov-jdk15on:jar:1.60:compile
[DEBUG] org.springframework.cloud:spring-cloud-netflix-hystrix:jar:2.2.0.RELEASE:compile
[DEBUG] org.springframework.boot:spring-boot-starter-aop:jar:2.2.1.RELEASE:compile
[DEBUG] org.springframework.cloud:spring-cloud-netflix-eureka-client:jar:2.2.0.RELEASE:compile
[DEBUG] com.netflix.eureka:eureka-client:jar:1.9.13:compile
[DEBUG] org.codehaus.jettison:jettison:jar:1.3.7:runtime
[DEBUG] stax:stax-api:jar:1.0.1:runtime
[DEBUG] com.netflix.netflix-commons:netflix-eventbus:jar:0.3.0:runtime
[DEBUG] com.netflix.netflix-commons:netflix-infix:jar:0.3.0:runtime
[DEBUG] commons-jxpath:commons-jxpath:jar:1.3:runtime
[DEBUG] joda-time:joda-time:jar:2.10.5:runtime (version managed from 2.3 by org.springframework.boot:spring-boot-dependencies:2.2.1.RELEASE)
[DEBUG] org.antlr:antlr-runtime:jar:3.4:runtime
[DEBUG] org.antlr:stringtemplate:jar:3.2.1:runtime
[DEBUG] antlr:antlr:jar:2.7.7:runtime
[DEBUG] org.apache.commons:commons-math:jar:2.2:runtime
[DEBUG] com.netflix.archaius:archaius-core:jar:0.7.6:compile
[DEBUG] javax.ws.rs:jsr311-api:jar:1.1.1:runtime
[DEBUG] com.netflix.servo:servo-core:jar:0.12.21:runtime
[DEBUG] com.sun.jersey:jersey-core:jar:1.19.1:runtime
[DEBUG] com.sun.jersey:jersey-client:jar:1.19.1:runtime
[DEBUG] com.sun.jersey.contribs:jersey-apache-client4:jar:1.19.1:runtime
[DEBUG] org.apache.httpcomponents:httpclient:jar:4.5.10:compile (version managed from 4.5.3 by org.springframework.boot:spring-boot-dependencies:2.2.1.RELEASE)
在输入里面搜索 webmvc ,找到最上级后,在pom里排除掉即可,例如:
1 | <dependency> |