簡體   English   中英

如何使用ControllerAdvice注釋的類來處理不同類型的異常?

[英]How to have a class annotated by ControllerAdvice to handle different types of exceptions?

我有一個名為GlobalExceptionHandler的類,該類由ControllerAdvice注釋。 它可以正確處理所有NoHandlerFoundExceptions 我添加了一個新方法來處理InternalError異常,但它不處理此類異常。 因此,我仍然收到HTTP Status 500

根據此鏈接 ,異常類500 (Internal Server Error)ConversionNotSupportedException

我嘗試了以下代碼,但沒有捕獲內部服務器錯誤。

1個

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleExceptionNotFound(NoHandlerFoundException ex) {
        System.err.println("not found");
        return "redirect:/error";
    }

    @ExceptionHandler(ConversionNotSupportedException.class)
//  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleExceptionsInternalError(ConversionNotSupportedException ex) {
        ex.printStackTrace();
        System.err.println("internal error");
        return "redirect:/error";
    }

}

2

@ExceptionHandler(ConversionNotSupportedException.class)
public String handleExceptionsInternalError(HttpServletRequest req, ConversionNotSupportedException ex) {

3

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleExceptionsInternalError(ConversionNotSupportedException ex) {

我需要處理的異常如下:

HTTP Status 500 - Could not resolve view with name 'user' in servlet with name 'myproject'
javax.servlet.ServletException: Could not resolve view with name 'user' in servlet with name 'myproject'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1211)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

看起來您的異常處理程序正在返回一個無效的視圖,這會在調用異常處理程序的代碼中導致異常。 請參閱DispatcherServlet.processDispatchResult

在這種情況下,異常是從DispatcherServlet本身引發的,並且處理異常的唯一方法是通過web.xml

您需要修復異常處理程序才能返回有效視圖。 使用@ExceptionHandler(Exception.class)處理的其他答案是有效的,可用於處理從控制器拋出的異常。

更新 :基於@Jack的評論,該異常是由控制器返回不存在的視圖(而不是我最初認為的異常處理程序)引起的。 返回視圖后,容器將調用視圖解析器來呈現它,如果拋出異常,則將不會調用異常處理程序(請參見thisthis ),最簡單的解決方案是在web.xml中處理它。

另一個選擇是重寫視圖resolver.resolveViewName,比如說您正在使用InternalResourceViewResolver ,它看起來可能像這樣:

public class CustomViewResolver extends InternalResourceViewResolver {
    @Override
    public View resolveViewName(String viewName,
                            Locale locale) throws Exception {
        try{
            return super.resolveViewName();
        } catch (Exception ex) {
            //log
            return new InternalResourceView("urlForErrorView");
       }
    }
}

似乎控制器建議無法處理此類異常。 我在我的web.xml添加了以下代碼來處理此異常。 如果您有更好的解決方案,請告訴我。 謝謝。

<error-page>
    <error-code>500</error-code>
    <location>/errorhandler</location>
</error-page>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM