If you are using Spring MVC framework for your application development, then you might have encountered the following error somewhere during the development cycle

java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public org.springframework.web.servlet.ModelAndView com.codippa.ErrorHandler.appError(com.codippa.exception.CustomException), public org.springframework.web.servlet.ModelAndView com.codippa.ErrorHandler.unknownError(java.lang.Exception)}.
at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.addExceptionMapping(ExceptionHandlerMethodResolver.java:108)
at org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.(ExceptionHandlerMethodResolver.java:75)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.initExceptionHandlerAdviceCache(ExceptionHandlerExceptionResolver.java:264)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.afterPropertiesSet(ExceptionHandlerExceptionResolver.java:240)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)

Reason

The application is using custom exception handler methods from Spring exception handling mechanism.
These methods are annotated with @ExceptionHandler annotation which declares the type of exception which will be handled by this method.
Example of an exception handler method is

@ExceptionHandler(Exception.class)
public ModelAndView unknownError(Exception e) {
   ModelAndView errorPage = new ModelAndView("errorpage");
   return errorPage;
}

Now the reason due to which above error will arise is that there are multiple exception handler methods which are handling the same exception type.
Consider the below example

@ControllerAdvice
public class ExceptionResolver { 

 /**
  * Handles java.lang.Exception
  */
  @ExceptionHandler(Exception.class)
  public ModelAndView unknownError(Exception e) {
    ModelAndView errorPage = new ModelAndView("errorpage");
    errorPage.addObject("exception", e);
    return errorPage;
  }

 /**
  * Ambiguity..This one also handles java.lang.Exception
  */
  @ExceptionHandler(Exception.class)
  public ModelAndView handleException(Exception e) {
    ModelAndView errorPage = new ModelAndView("errorpage");
    errorPage.addObject("exception", e);
    return errorPage;
  }
}

In the above code, there are two methods annotated with @ExceptionHandler and both handle the same exception type(Exception.class).
Above declaration of methods will definitely produce the error mentioned at the beginning.

Solution

Ensure that all the exception handler methods handle distinct types of exceptions, that is, all the methods annotated with @ExceptionHandler annotation handle different type of exceptions.
Above code which produced the error when modified as below resolves the Ambiguous @ExceptionHandler method mapped error.

@ControllerAdvice
public class ExceptionResolver { 

 /**
  * Handles java.lang.Exception
  */
  @ExceptionHandler(Exception.class)
  public ModelAndView unknownError(Exception e) {
    ModelAndView errorPage = new ModelAndView("errorpage");
    errorPage.addObject("exception", e);
    return errorPage;
  }

 /**
  * Handles java.io.IOException
  */
  @ExceptionHandler(IOException.class)
  public ModelAndView handleException(Exception e) {
    ModelAndView errorPage = new ModelAndView("errorpage");
    errorPage.addObject("exception", e);
    return errorPage;
  }
}

Observe carefully that both the exception handler methods now handle java exceptions, each of different type:
One handles java.lang.Exception, and
other handles java.io.IOException.

Hope this post helped to resolve your problem.
Keep visiting !!!

0
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.