Error creating bean with name 'handlerExceptionResolver' defined in class path resource 在类路径资源中定义的名称为"handlerExceptionResolver"的bean创建时出现错误。

4

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

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 注解并编译它时,它也能正常工作。
2个回答

7
错误信息很明显,它提示“模棱两可的异常处理方法”,因为您有两个映射到相同异常(Exception.class) 的方法。 handleAllUserException:处理所有用户异常。
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllUserException(Exception ex, WebRequest request)  

处理用户未找到异常

 @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应用程序中创建名为handlerExceptionResolver的bean时发生的BeanCreationException异常。该异常是由IllegalStateException引起的,该异常表示为MethodArgumentNotValidException映射了一个模糊的@ExceptionHandler方法。
似乎有两个使用@ExceptionHandler注解的方法来处理MethodArgumentNotValidException:com.congdat.springbootrestapi.exception.GlobalExceptionHandler.handleMethodArgumentNotValid和org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException。
要解决此问题,您需要确保每个异常类型只映射一个@ExceptionHandler方法。您可以通过删除其中一个@ExceptionHandler方法或使用@Order注解为@ExceptionHandler方法指定顺序来实现此目的。
另一个可能的解决方案是覆盖handlerExceptionResolver bean定义,并提供自己的ExceptionHandlerExceptionResolver,其中包含一个自定义的ExceptionHandlerMethodResolver来解决模糊映射的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接