spring处理异常报错: Ambiguous @ExceptionHandler method mapped for [class ...

在使用Springmvc处理异常的时候

使用了@ExceptionHandler

并且在一个类里面的@ExceptionHandler里面的异常类型一致,就会导致报错。

 

只需要将异常的类型改为不一样的就行了

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是由于在 SpringBoot 项目中,存在多个异常处理方法处理同一种异常,导致 Spring 无法确定该使用哪个方法来处理异常。解决这个问题的方法是在异常处理方法上添加不同的参数,以区分这些方法。 比如,假设你有两个方法用来处理 Exception 异常: ```java @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { return new ResponseEntity<>("Exception occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(Exception.class) public ResponseEntity<String> handleOtherException(Exception e) { return new ResponseEntity<>("Other exception occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } ``` 这两个方法都处理 Exception 异常,导致 Spring 无法确定使用哪个方法。你可以在这两个方法上添加不同的参数,例如: ```java @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e, HttpServletRequest request) { return new ResponseEntity<>("Exception occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(Exception.class) public ResponseEntity<String> handleOtherException(Exception e, HttpServletResponse response) { return new ResponseEntity<>("Other exception occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } ``` 这样,Spring 就可以根据传递的参数类型来确定使用哪个方法来处理异常了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值