Spring Boot引入WireMock后出现JettyWebServer依赖冲突报错
Spring Boot + WireMock 启动报错:NoClassDefFoundError 缺失RequestLog$Writer类
问题描述
Spring Boot 2.6.6项目中引入WireMock 2.27.2依赖后,启动时无法启动Web服务器,抛出NoClassDefFoundError,提示找不到org/eclipse/jetty/server/RequestLog$Writer类,导致jettyWebServerFactoryCustomizer Bean创建失败。
项目依赖配置
buildscript { ext { LOMBOK_VERSION = '1.18.22' SLF4J_VERSION = '1.7.30' SPRING_BOOT_VERSION = "2.6.6" SPRING_RETRY_VERSION = "1.3.1" JUNIT_JUPITER_VERSION = "5.8.2" MOCKITO_JUNIT_JUPITER_VERSION = "4.3.1" MSSQL_JDBC_VERSION = "9.4.1.jre11" LOGSTASH_LOGBACK_ENCODER_VERSION = "7.2" TESTCONTAINERS_VERSION = "1.17.6" WIREMOCK_VERSION = "2.27.2" TEST_GSON_VERSION = "2.10" } } plugins { id 'java' id "org.springframework.boot" version "${SPRING_BOOT_VERSION}" } springBoot { buildInfo() } repositories { maven { url = 'xxx' } } dependencies { // As this whole project is a test, I´m including everything as implementation implementation "org.springframework.boot:spring-boot-starter:${SPRING_BOOT_VERSION}" implementation "net.logstash.logback:logstash-logback-encoder:${LOGSTASH_LOGBACK_ENCODER_VERSION}" implementation 'com.microsoft.sqlserver:mssql-jdbc:12.2.0.jre11' implementation "org.springframework.boot:spring-boot-starter-test:${SPRING_BOOT_VERSION}" implementation "org.testcontainers:testcontainers:${TESTCONTAINERS_VERSION}" implementation "org.testcontainers:junit-jupiter:${TESTCONTAINERS_VERSION}" implementation "com.google.code.gson:gson:${TEST_GSON_VERSION}" testImplementation "com.github.tomakehurst:wiremock:${WIREMOCK_VERSION}" testImplementation "org.junit.jupiter:junit-jupiter-engine:${JUNIT_JUPITER_VERSION}" testImplementation "org.mockito:mockito-junit-jupiter:${MOCKITO_JUNIT_JUPITER_VERSION}" } test { useJUnitPlatform() }
报错堆栈信息
Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jettyWebServerFactoryCustomizer' defined in class path resource [org/springframework/boot/autoconfigure/web/embedded/EmbeddedWebServerFactoryCustomizerAutoConfiguration$JettyWebServerFactoryCustomizerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer]: Factory method 'jettyWebServerFactoryCustomizer' threw exception; nested exception is java.lang.NoClassDefFoundError: org/eclipse/jetty/server/RequestLog$Writer at app//org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) at app//org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) at app//org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at app//org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) at app//org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) at app//org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at app//org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:136) at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
解决方案
1. 排除WireMock自带的Jetty依赖
WireMock 2.27.2自带的Jetty版本与Spring Boot 2.6.6的Jetty版本冲突,导致类缺失。修改依赖配置,排除WireMock中的Jetty相关模块,强制使用Spring Boot管理的Jetty版本:
testImplementation("com.github.tomakehurst:wiremock:${WIREMOCK_VERSION}") { exclude group: 'org.eclipse.jetty' exclude group: 'org.eclipse.jetty.websocket' }
2. 升级WireMock到兼容版本
WireMock 2.27.2版本较旧,建议升级到适配Spring Boot 2.6.x的版本(如2.35.0),新版本会调整依赖的Jetty版本,减少冲突:
// 在ext块中修改版本 WIREMOCK_VERSION = "2.35.0"
3. 统一指定Jetty版本
在buildscript的ext块中添加Spring Boot默认的Jetty版本,强制所有依赖使用该版本:
ext { // ... 其他版本配置 JETTY_VERSION = "9.4.46.v20220331" // Spring Boot 2.6.6默认Jetty版本 }
内容的提问来源于stack exchange,提问作者emoleumassi
相关产品推荐
相关产品推荐

