创建在类路径资源中定义的名为“handlerExceptionResolver”的 bean 时出错

问题描述 投票:0回答:3

我有一个简单的项目,我在其中创建了自定义异常处理。有趣的是,当我编译这个项目时,它给了我一个错误。

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 注释并编译时,它工作正常。

java spring exception
3个回答
7
投票

错误消息是有意义的,它说 Ambigitude

@ExceptionHandler
方法,因为你有两个方法映射到同一个异常,即
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)  

0
投票

这是在 Spring Boot 应用程序中创建名为

BeanCreationException
的 bean 时发生的
handlerExceptionResolver
的堆栈跟踪。该异常是由
IllegalStateException
引起的,这表明为
@ExceptionHandler
映射了不明确的
MethodArgumentNotValidException
方法。

@ExceptionHandler
MethodArgumentNotValidException
注释的方法好像有两种:
com.congdat.springbootrestapi.exception.GlobalExceptionHandler.handleMethodArgumentNotValid
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException

要解决此问题,您需要确保每种异常类型仅映射一个

@ExceptionHandler
方法。您可以通过删除其中一个
@ExceptionHandler
方法或使用
@ExceptionHandler
注释指定
@Order
方法的顺序来完成此操作。

另一种可能的解决方案是覆盖

handlerExceptionResolver
bean 定义并提供您自己的
ExceptionHandlerExceptionResolver
以及自定义的
ExceptionHandlerMethodResolver
来解决不明确的映射。


0
投票

我遇到同样的错误,我尝试这个

public ResponseEntity<Object> handleMethodArgumentNotValid(HttpServletRequest req,MethodArgumentNotValidException ex){
    ErrorResponse response = new ErrorResponse(HttpStatus.BAD_REQUEST);
    response.setMessage("Validation error: " + ex.getMessage() + " " + req.getRequestURI());
    return buildResponseEntity(response);
}

@Override
protected ResponseEntity<java.lang.Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
    return handleMethodArgumentNotValid(((ServletWebRequest) request).getRequest(), ex);
}

它对我有用

© www.soinside.com 2019 - 2024. All rights reserved.