2015-04-17 106 views
2

我工作的示例演示應用程序的異常處理在春季MVC.I我試圖Exception Handling With @ControllerAdvice@ControllerAdvice異常處理方法不會被調用

我也跟着步驟在this鏈接描述。

但是當我運行我的應用程序出現以下錯誤

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

對於以下更多細節的班我的工作

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 
} 

控制器類

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

這裏是GlobalExceptionHandler類

GlobalExceptionHandler.java

@ControllerAdvice 
public class GlobalExceptionHandler { 

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

調度-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> 

我上面的應用程序運行與http://localhost:8080/SpringExceptionHandling/exception/error這個網址

但我得到下面的異常

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) 

我經歷@ControllerAdvice exception handler method are not get called此鏈接,但仍堅持同樣的問題 請幫我以獲得解決方案。我沒有得到這個例外的真正原因。

回答

5

您在您的異常處理程序中缺少@EnableWebMvc註釋。你的異常處理程序應該是這樣的。

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

} 

此外,您還需要告訴異常處理程序方法哪種異常要捕獲哪種方法。最後你可以指定一個方法,它可以捕獲所有其他異常通過Exception.class。 您爲我發佈的代碼片段。希望它能解決你的問題。

+0

仍然無法正常工作。是否這個代碼在你身邊?如果是的話,請你分享你正在使用的罐子名稱,我想我錯過了一些jar文件 – rachana

+0

@rachana,spring-core,spring-web,spring-webmvc。你在使用Maven項目嗎? –

+0

不,我沒有使用Maven項目。我在我的4.0.0版本的項目中使用了所有這些jar文件 – rachana

1

您需要聲明異常,以便該方法可以映射到異常類型。你有兩種可能,要麼狀態異常作爲參數

public void notFount(ApplicationException exception){ 

添加類型爲您@ExceptionHandler標註值屬性

@ExceptionHandler(value = ApplicationException.class) 

此外,你最可能有配置問題,因爲你發佈的錯誤GlobalExceptionHandler,servlet不會啓動,明確指出沒有異常類型映射到該方法。確保您正在掃描GlobalExceptionHandler所在的包裝,看起來很可能是您的問題

+0

我已經嘗試過了,但我仍然得到相同的exception.The控制器沒有得到調用 – rachana

+0

地圖的例外在我的答覆中提到的方式方法,並確保你掃描了'com.test.core'包,剩下的看起來很好,讓我知道如果適合你 –

+0

正如你建議我檢查我的配置,我已經添加了''元素在我的配置中,也添加了'com.test.core'包。我已經更新了我的問題請看看。 – rachana

0

在我的情況下,處理程序方法不會被調用,因爲方法簽名中有附加參數。

不工作:

@ExceptionHandler(NoHandlerFoundException.class) 
public ModelAndView handle404(HttpServletRequest request, Exception e, @RequestHeader(value = "referer", required = false) final String referer) { 
    ... 
} 

referer參數是什麼原因造成的處理方法沒有被調用。刪除參數後問題解決。

作品:

@ExceptionHandler(NoHandlerFoundException.class) 
public ModelAndView handle404(HttpServletRequest request, Exception e) { 
    ... 
}