无法实例化[org.springframework.web.servlet.HandlerExceptionResolver];映射不明确的@ExceptionHandler方法

时间:2016-10-22 14:30:13

标签: spring spring-mvc exceptionhandler handlerexceptionresolver

我的 GlobalExceptionHandlerController 类:

@ControllerAdvice
public class GlobalExceptionHandlerController {

    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleError404(HttpServletRequest request, Exception e) {
        return "404";
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handleError400(HttpServletRequest request, Exception e) {
        return "errors1";
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public String handleError403(HttpServletRequest request, Exception e) {
        return "errors2";
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleError500(HttpServletRequest request, Exception e) {
        return "errors3";
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
    public String handleError429(HttpServletRequest request, Exception e) {
        return "errors4";
    }

}

我收到了这个错误:

Caused by: 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 java.lang.String com.fussa.fyby.configuration.GlobalExceptionHandlerController.handleError403(javax.servlet.http.HttpServletRequest,java.lang.Exception), public java.lang.String com.fussa.fyby.configuration.GlobalExceptionHandlerController.handleError400(javax.servlet.http.HttpServletRequest,java.lang.Exception)}
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 24 more
Caused by: java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public java.lang.String com.fussa.fyby.configuration.GlobalExceptionHandlerController.handleError403(javax.servlet.http.HttpServletRequest,java.lang.Exception), public java.lang.String com.fussa.fyby.configuration.GlobalExceptionHandlerController.handleError400(javax.servlet.http.HttpServletRequest,java.lang.Exception)}
    at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.addExceptionMapping(ExceptionHandlerMethodResolver.java:109)
    at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.<init>(ExceptionHandlerMethodResolver.java:76)
    at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.initExceptionHandlerAdviceCache(ExceptionHandlerExceptionResolver.java:265)
    at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.afterPropertiesSet(ExceptionHandlerExceptionResolver.java:241)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHandlerExceptionResolvers(WebMvcConfigurationSupport.java:879)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.handlerExceptionResolver(WebMvcConfigurationSupport.java:822)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$400a1941.CGLIB$handlerExceptionResolver$41(<generated>)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$400a1941$$FastClassBySpringCGLIB$$2aaff7a.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$400a1941.handlerExceptionResolver(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 25 more

感谢您的任何建议..

2 个答案:

答案 0 :(得分:1)

Spring无法猜测它应该针对哪个异常使用哪种处理程序方法。因为你没有定义它们。

为每个适当的方法设置ExceptionHandler注释的值,并确保没有重复项。

答案 1 :(得分:0)

mh-dev所示,我为每个ExceptionHandler注释定义了异常:

@ControllerAdvice
public class GlobalExceptionHandlerController {

    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleError404(HttpServletRequest request, Exception e) {
        return "404";
    }

    @ExceptionHandler({ HttpClientErrorException.class,Throwable.class  })
    @ResponseStatus
    public String handleError4xx(HttpServletRequest request, Exception e) {
        return "errors4";
    }

    @ExceptionHandler(HttpServerErrorException.class)
    @ResponseStatus
    public String handleError5xx(HttpServletRequest request, Exception e) {
        return "errors5";
    }

}