SpringBoot项目启动失败,Ambiguous mapping. Cannot map ‘xxxController‘ method

本文档记录了一个SpringBoot项目在2.1.15.RELEASE版本中启动失败的问题。问题源于两个Controller中存在相同访问路径的工作方法,导致Ambiguous mapping异常。解决方案是修改Controller的基础访问路径,确保方法映射的唯一性,从而成功启动项目。

目录

 

项目场景:

问题描述:

原因分析:

解决方案:


项目场景:

SpringBoot 2.1.15.RELEASE


问题描述:

SpringBoot项目启动失败了,后台日志没有Error的异常堆栈信息。详细查看了日志后,发现有WARN级别的信息:

[main] WARN  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/D:/tfb-work/maven-repositories/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/D:/tfb-work/maven-repositories/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'xxxController' method 
public com.xxx.framework.web.controller.ResponseDto<> com.controller.XxxController.workListStatistics()
to {[/xxx/workListStatistics],methods=[POST],produces=[application/json;charset=UTF-8]}: There is already 'yyyController' bean method
public com.framework.web.controller.ResponseDto<> com.controller.yyy.YyyController.workListStatistics() mapped.

 

原因分析:

分析上面的错误信息, Ambiguous mapping. Cannot map 'xxxController' method ,意思是模糊的方法/路径匹配,XxxController.workListStatistics()这个方法和YyyController.workListStatistics()的方法都配置了一样的访问路径

查看了下这两个方法的访问路径注解,果然是一样的。


解决方案:

按实际情况处理,如我这边是修改两个Controller的基础访问路径。

注:原开发者设置了同一个路径,是因为这两个Controller不在同一个jar包里,本意是不允许相互依赖,可是没有提前声明。优化后,更改了这两个Controller的基础访问路径。

`Ambiguous mapping. Cannot map 'chaugnyezhezhuceController' method` 错误通常是因为存在两个方法具有相同的映射路径和 HTTP 方法,在 Spring 尝试创建 `requestMappingHandlerMapping Bean` 时引发了冲突 [^4]。以下是一些可能的解决方法: ### 检查映射路径 确保 `chaugnyezhezhuceController` 类中没有两个方法使用了相同的 `@RequestMapping`、`@GetMapping`、`@PostMapping` 等注解,且路径和 HTTP 方法一致。例如: ```java @RestController public class ChaugnyezhezhuceController { // 错误示例,两个方法映射路径和 HTTP 方法相同 @GetMapping("/test") public String test1() { return "Test 1"; } @GetMapping("/test") public String test2() { return "Test 2"; } } ``` 要修改为不同的路径: ```java @RestController public class ChaugnyezhezhuceController { @GetMapping("/test1") public String test1() { return "Test 1"; } @GetMapping("/test2") public String test2() { return "Test 2"; } } ``` ### 检查不同控制器中的映射 除了当前控制器,还要检查其他控制器中是否有相同的映射路径。比如,可能有另一个控制器也有 `/test` 这个映射路径。 ### 检查全局配置 有时候,全局的配置文件或者自动配置可能会导致映射冲突。检查 `application.properties` 或 `application.yml` 中的配置,确保没有重复的配置影响了映射。 ### 检查继承关系 如果 `chaugnyezhezhuceController` 有继承关系,要检查父类中是否存在相同的映射路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值