Vijay
Vijay

Reputation: 35

Error creating bean with name 'handlerExceptionResolver' defined in class path resource

I have a simple project where I have created custom exception handling. Funny thing is when I compile this project it gives me an error.

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);
    }

}

When I comment 'handleUserNotFoundException' function it works fine however when I add this method it gives me that error. Why would Spring cannot handle it? Any help will be greatly appreciated.

I removed function 'handleUserNotFoundException' and it works fine.. Also when i just remove ExceptionHandler annotation and compile it works fine.

Upvotes: 4

Views: 22195

Answers (3)

Haitam-Elgharras
Haitam-Elgharras

Reputation: 138

I had the same error and I tried this:

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);
}

It works for me.

Upvotes: 0

Mr.Zero
Mr.Zero

Reputation: 1

This is a stack trace of a BeanCreationException that occurred while creating a bean named handlerExceptionResolver in a Spring Boot application. The exception is caused by an IllegalStateException which indicates that an ambiguous @ExceptionHandler method was mapped for a MethodArgumentNotValidException.

It seems that there are two methods annotated with @ExceptionHandler for MethodArgumentNotValidException: com.congdat.springbootrestapi.exception.GlobalExceptionHandler.handleMethodArgumentNotValid and org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException.

To resolve this issue, you need to make sure that only one @ExceptionHandler method is mapped for each exception type. You can do this by removing one of the @ExceptionHandler methods or by specifying an order for the @ExceptionHandler methods using the @Order annotation.

Another possible solution is to override the handlerExceptionResolver bean definition and provide your own ExceptionHandlerExceptionResolver with a customized ExceptionHandlerMethodResolver that resolves the ambiguous mapping.

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 40068

The error message is meaningful, it's says Ambiguous @ExceptionHandler method because you have two method mapped for same exception which is 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)  

Both the methods are mapped for Exception.class which is Ambiguous for container thread to pick at runtime, change the handleUserNotFoundException method mapping to UnknownException

@ExceptionHandler(UnknownException.class)
public final ResponseEntity<Object> handleUserNotFoundException(UnknownException ex, WebRequest request)  

Upvotes: 8

Related Questions