首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建类路径资源中定义的名为“handlerExceptionResolver”的bean时出错

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

Stack Overflow用户
提问于 2019-05-14 01:44:12
回答 1查看 12.3K关注 0票数 4

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

代码语言:javascript
复制
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]
代码语言:javascript
复制
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‘函数时,它工作得很好,但是当我添加这个方法时,它会给出这个错误。为什么春不能处理它?任何帮助都将不胜感激。

我删除了函数'handleUserNotFoundException‘,它工作得很好。另外,当我移除ExceptionHandler注释并编译它时,它工作得很好。

EN

回答 1

Stack Overflow用户

发布于 2019-05-14 01:52:40

错误消息是有意义的,它说是不明确的@ExceptionHandler方法,因为您有两个方法映射到相同的异常,即Exception.class

handleAllUserException

代码语言:javascript
复制
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllUserException(Exception ex, WebRequest request)  

handleUserNotFoundException

代码语言:javascript
复制
 @ExceptionHandler(Exception.class)
 public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request)  

这两种方法都映射为Exception.class,这对于容器线程在运行时要选择的对象来说是不明确的,因此将handleUserNotFoundException方法映射为UnknownException

代码语言:javascript
复制
@ExceptionHandler(UnknownException.class)
public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request)  
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56121931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档