2012-11-13 45 views
11

我使用@ExceptionHandler來處理我的web應用程序拋出的異常,在我的情況下,我的應用程序返回JSON響應HTTP status錯誤響應到客戶端。處理與彈簧控制器錯誤404

不過,我想弄清楚如何處理error 404返回喜歡與一個由處理類似的JSON響應@ExceptionHandler

更新:

我的意思是,當一個URL是不存在被訪問

回答

2

最簡單的方法添加以下代碼,找出是使用以下命令:

@ExceptionHandler(Throwable.class) 
    public String handleAnyException(Throwable ex, HttpServletRequest request) { 
    return ClassUtils.getShortName(ex.getClass()); 
    } 

如果URL DispatcherServlet的範圍則造成錯誤輸入或其他任何東西會被抓住任何404內通過這種方法,但如果輸入的網址是超越的DispatcherServlet的URL映射,那麼你必須要麼使用:

<error-page> 
    <exception-type>404</exception-type> 
    <location>/404error.html</location> 
</error-page> 

將「/」映射提供給DispatcherServlet映射URL以處理特定服務器實例的所有映射。

+0

這對我不起作用:我爲我的DispatcherServlet設置了「/」映射,但該方法沒有觸發... – davioooh

+0

請求是在相同的類還是父類中進行的? – Abhi

+0

不,我試圖設置一個全局處理程序...(http://stackoverflow.com/q/21884740/1061499) – davioooh

4

隨着彈簧> 3.0使用@ResponseStatus

@ResponseStatus(value = HttpStatus.NOT_FOUND) 
    public class ResourceNotFoundException extends RuntimeException { 
    ... 
} 

    @Controller 
    public class MyController { 
    @RequestMapping..... 
    public void handleCall() { 
     if (isFound()) { 
     // do some stuff 
     } 
     else { 
       throw new ResourceNotFoundException(); 
     } 
    } 
} 
+0

我的意思是,當一個不存在的URL被訪問時 – xybrek

+0

404或Not Found錯誤消息是一個HTTP標準響應代碼,指示客戶端能夠與服務器通信,但服務器找不到請求的內容。 –

+0

我的意思是,例如這個URL存在:/ rest/doc/1 <=存在,然而/ rest/doc/1/notexist <=不存在(這就是我的意思) – xybrek

1

您可以使用servlet標準方式來處理404錯誤。在web.xml

<error-page> 
    <exception-type>404</exception-type> 
    <location>/404error.html</location> 
</error-page> 
35

我使用spring 4.0和java配置。我的工作代碼爲:

@ControllerAdvice 
public class MyExceptionController { 
    @ExceptionHandler(NoHandlerFoundException.class) 
    public ModelAndView handleError404(HttpServletRequest request, Exception e) { 
      ModelAndView mav = new ModelAndView("/404"); 
      mav.addObject("exception", e); 
      //mav.addObject("errorcode", "404"); 
      return mav; 
    } 
} 

在JSP:

<div class="http-error-container"> 
     <h1>HTTP Status 404 - Page Not Found</h1> 
     <p class="message-text">The page you requested is not available. You might try returning to the <a href="<c:url value="/"/>">home page</a>.</p> 
    </div> 

對於初始參數配置:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 
    @Override 
    public void customizeRegistration(ServletRegistration.Dynamic registration) { 
     registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); 
    } 
} 

或XML:

<servlet> 
    <servlet-name>rest-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>throwExceptionIfNoHandlerFound</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</servlet> 

另請參見:Spring MVC Spring Security and Error Handling

+0

這是最好的答案,因爲使用Spring處理所有事情可以讓您提供所有的功能處理Java與靜態html頁面的錯誤。 – dev

+0

感謝你們,現在看起來是正確的做事方式,但它並不適合我。如果你有時間,有沒有機會告訴我我做錯了什麼? http://stackoverflow.com/questions/28750038/returning-json-with-spring-on-404-in-xml-free-project –

+0

如果你有一個資源處理程序配置和資源找不到,那麼這doesn沒有工作。不幸的是,ResourceHttpRequestHandler發送一個404響應,而不是拋出某種你可以捕獲的資源異常。 – 2016-05-06 19:22:33

1
public final class ResourceNotFoundException extends RuntimeException { 

} 


@ControllerAdvice 
public class AppExceptionHandler { 
    @ExceptionHandler(ResourceNotFoundException.class) 
    @ResponseStatus(HttpStatus.NOT_FOUND) 
    public String handleNotFound() { 
     return "404"; 
    } 
} 

只需定義一個Exception,一個ExceptionHandler,從您的業務代碼控制器中拋出異常。

+0

這在我的Spring 4應用程序中完美工作,並且是爲Spring設置狀態代碼的適當解決方案像'NoHandlerFoundException'這樣的原生異常。 – vallismortis