You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Gateway启动报错:requestMappingHandlerAdapter Bean重复定义(求非覆盖方案)

问题:Spring Cloud Gateway启动时Bean定义冲突(requestMappingHandlerAdapter)

项目依赖(Gradle)

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix:2.2.10.RELEASE'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    implementation 'com.netflix.eureka:eureka-core'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
    implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
    implementation 'commons-validator:commons-validator:1.7'
    implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    implementation 'org.apache.commons:commons-lang3:3.12.0'
    implementation 'com.github.ben-manes.caffeine:caffeine'
    implementation 'com.auth0:jwks-rsa:0.21.2'
    implementation 'com.google.http-client:google-http-client-jackson2:1.42.2'
    implementation 'org.springframework:spring-context-support:5.3.23'
    implementation 'net.logstash.logback:logstash-logback-encoder:7.2'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.4'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'    
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

启动错误信息

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'requestMappingHandlerAdapter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class], could not be registered. A bean with that name has already been defined in org.springframework.web.reactive.config.DelegatingWebFluxConfiguration and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解决方法(不开启Bean覆盖)

1. 定位冲突根源

Spring Cloud Gateway基于WebFlux(响应式Web框架),错误显示Servlet栈的WebMvcAutoConfiguration自动配置类被加载,导致同名称的Bean与WebFlux的Bean冲突。核心原因是某个依赖间接引入了Servlet栈的spring-boot-starter-web模块。

2. 排查依赖树

执行Gradle命令查看完整依赖树,找出引入spring-boot-starter-web的来源:

./gradlew dependencies --configuration implementation

在输出结果中搜索spring-boot-starter-web,定位到对应的依赖节点。

3. 排除冲突依赖

找到来源后,在对应依赖中排除spring-boot-starter-web。例如,如果是com.netflix.eureka:eureka-core引入的,修改依赖声明:

implementation('com.netflix.eureka:eureka-core') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web'
}

其他依赖导致的冲突,同理调整排除规则即可。

4. 移除重复依赖

你的依赖中com.github.ben-manes.caffeine被重复声明(一个带版本,一个不带),建议保留带版本的声明,删除重复项:

// 保留指定版本的依赖
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
// 删除下面这条重复声明
// implementation 'com.github.ben-manes.caffeine:caffeine'

5. 备选方案:直接排除WebMvc自动配置

如果暂时无法定位依赖来源,可以在启动类上直接排除WebMvc的自动配置类,阻止其加载:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;

@SpringBootApplication(exclude = WebMvcAutoConfiguration.class)
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

内容的提问来源于stack exchange,提问作者Peter Penzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:01:07