我有一个简单的项目,在其中创建了自定义异常处理。有趣的是,当我编译该项目时,它给了我一个错误。
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批注并进行编译时,工作正常。
答案 0 :(得分:0)
该错误消息是有意义的,它说是模糊的@ExceptionHandler
方法,因为您为相同的异常(Exception.class
)映射了两个方法
handleAllUserException
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllUserException(Exception ex, WebRequest request)
handleUserNotFoundException
@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)