Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ControllerAdvice exception handler method not get called

I am working on sample demo application for Exception Handling in Spring MVC.I am trying Exception Handling With @ControllerAdvice

I followed steps as describe in this link.

But when i run my application i get the following error

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException

For more details following are the classes I am working on

ApplicationException.java

public class ApplicationException extends RuntimeException{


/**
 * 
 */
private static final long serialVersionUID = -9061684361606685158L;
private ErrorStatus errorStatus;
private int msgNumber;
private Object []args;

public ApplicationException(){}

public ApplicationException(ErrorStatus errorStatus, int msgNumber,
        Object[] args) {
    this.errorStatus = errorStatus;
    this.msgNumber = msgNumber;
    this.args = args;
  }
    //getter setters
}

Controller class

ApplicationExceptionController.java

 @Controller
@RequestMapping("/exception")
public class ApplicationExceptionController {

@RequestMapping(value="/error",method=RequestMethod.GET)
@ResponseBody
public void helloException() 
{
    throw new ApplicationException(ErrorStatus.NotFound,1,new Object[]{"Website"});

  //here ErrorStatus is a enum class
}  
}

And here is the GlobalExceptionHandler class

GlobalExceptionHandler.java

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public void notFount(){
    System.out.println("----------CaughtApplicationException-----------");
}
}

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:p="http://www.springframework.org/schema/p"      
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:mvc="http://www.springframework.org/schema/mvc" 

xsi:schemaLocation=
    "http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<context:component-scan  base-package="com.test.controller,com.test.core" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/jsp/" />  
    <property name="suffix" value=".jsp" />   
</bean>  

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
     <list> <ref bean="jsonConverter"/></list>
    </property>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
     <property name="supportedMediaTypes" value="application/json" />
</bean>

I run above application with http://localhost:8080/SpringExceptionHandling/exception/error this url

But I get the following Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

com.test.core.ApplicationException
com.test.controller.ApplicationExceptionController.helloException(ApplicationExceptionController.java:19)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I go through @ControllerAdvice exception handler method are not get called this link but still stuck with the same problem Please help me to get the solution.I am not getting the actual cause for this exception.

like image 235
rachana Avatar asked Apr 17 '15 11:04

rachana


People also ask

How do you define an exception handler method in Spring MVC?

Spring MVC Framework provides following ways to help us achieving robust exception handling. Controller Based - We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation. This annotation takes Exception class as argument.

How do you handle exceptions in a Spring web application?

Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.

What is true about the @ExceptionHandler and @ControllerAdvice annotations?

The@ControllerAdvice annotation allows us to consolidate our multiple, scattered @ExceptionHandlers from before into a single, global error handling component. The actual mechanism is extremely simple but also very flexible: It gives us full control over the body of the response as well as the status code.

What is @RestControllerAdvice?

@RestControllerAdvice is the combination of both @ControllerAdvice and @ResponseBody. The @ControllerAdvice annotation was first introduced in Spring 3.2. We can use the @ControllerAdvice annotation for handling exceptions in the RESTful Services but we need to add @ResponseBody separately.


1 Answers

You are missing @EnableWebMvc annotation in your Exception handler. Your Exception handler should be like this.

@EnableWebMvc
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({ApplicationException.class})
    public void notFount(){
        System.out.println("----------CaughtApplicationException-----------");
    }

    @ExceptionHandler({Exception.class})
    public void notFountGlobal(){
        System.out.println("----------CaughtApplicationException-----------");
    }

}

Also you need to tell exception handler method which exception you want to catch in which method. At last you can specify a method which can caught all other exception passing it Exception.class. The code snippet you post work for me. Hopefully it will resolve your problem.

like image 82
Vishal Singh Avatar answered Oct 13 '22 01:10

Vishal Singh