java - 在类路径资源中定义名称为“handlerExceptionResolver”的 bean 创建错误
问题描述
我有一个简单的项目,我在其中创建了自定义异常处理。有趣的是,当我编译这个项目时,它给了我一个错误。
Error creating bean with name 'handlerExceptionResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception; nested exception is java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public final org.springframework.http.ResponseEntity com.winwinit.rest.microservices.restfulwebservices.Exception.CustomizedEntityExceptionResponse.handleAllUserException(java.lang.Exception,org.springframework.web.context.request.WebRequest), public final org.springframework.http.ResponseEntity com.winwinit.rest.microservices.restfulwebservices.Exception.CustomizedEntityExceptionResponse.handleAllException(java.lang.Exception,org.springframework.web.context.request.WebRequest)}
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
package com.winwinit.rest.microservices.restfulwebservices.Exception;
import java.util.Date;
import org.omg.CORBA.portable.UnknownException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.winwinit.rest.microservices.restfulwebservices.User.UserNotFoundException;
@ControllerAdvice
@RestController
public class CustomizedEntityExceptionResponse extends
ResponseEntityExceptionHandler{
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllException(Exception ex, WebRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllUserException(Exception ex, WebRequest request) {
exceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity(exceptionResponse, HttpStatus.NOT_FOUND);
}
}
当我评论'handleUserNotFoundException'函数时它工作正常但是当我添加这个方法时它给了我这个错误。为什么Spring不能处理它?任何帮助将不胜感激。
我删除了函数'handleUserNotFoundException',它工作正常。此外,当我删除 ExceptionHandler 注释并编译时,它工作正常。
解决方案
错误消息是有意义的,它说 Ambiguous @ExceptionHandler
method 因为您有两个方法映射为相同的异常,即Exception.class
处理所有用户异常
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllUserException(Exception ex, WebRequest request)
处理UserNotFoundException
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request)
这两种方法都被映射,Exception.class
容器线程在运行时选择不明确的方法,将handleUserNotFoundException
方法映射更改为UnknownException
@ExceptionHandler(UnknownException.class)
public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request)
推荐阅读
- flutter - 创建小部件的正确方法?
- node.js - 节点 js 管道数据到 kinesis 流
- postgresql - 不同版本上的pgsql主从复制错误
- python - 使用主键连接来自 2 个模型的字段
- elasticsearch - 有没有办法在弹性搜索中使用 Levenshtein 找到两个单词之间的距离和比率?
- python - 如何为我的聊天机器人应用程序修复 sqlite3.OperationalError?
- html - 为什么包含在基本标签类别中?
- file - 如何将字节转换为文件类型以便将其上传到服务器
- java - PICOContainer 未在构造函数中初始化依赖项
- mysql - 如何按顺序对两个 varchar 列进行排序