2011-08-29 37 views
5

Tôi hiện đang cố gắng sử dụng HandlerExceptionResolver để xử lý ngoại lệ trong dự án Spring MVC.Xử lý ngoại lệ MVC mùa xuân với HandlerExceptionResolver

Tôi muốn xử lý các ngoại lệ thông thường qua resolveException cũng như 404 qua handleNoSuchRequestHandlingMethod.

Tùy thuộc vào loại yêu cầu JSON hoặc văn bản/html, phản hồi ngoại lệ phải được trả về một cách thích hợp.

resolveException hoạt động ngay bây giờ.

Nhưng handleNoSuchRequestHandlingMethod đang khiến tôi đau đầu. Nó không bao giờ được gọi!

Theo docu phương pháp này nên được gọi là trên 404 lỗi

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html

Tôi đang làm gì sai ...

Đây là những gì tôi có cho đến nay.

public class JsonExceptionResolver implements HandlerExceptionResolver { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    public ModelAndView resolveException(HttpServletRequest request, 
    if (exception instanceof NoSuchRequestHandlingMethodException) { 
       return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException)    exception, request, response, handler); 
    } 
    ...     
    } 

    public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, 
     HttpServletRequest request, 
     HttpServletResponse response, 
     Object handler){ 

    logger.info("Handle my exception!!!"); 

    ModelAndView mav = new ModelAndView(); 
    boolean isJSON = request.getHeader("Accept").equals("application/json"); 

    if(isJSON){ 
    ... 

    }else{ 
    .. 
    } 

    return mav; 
    } 

} 

EDIT với DefaultHandlerExceptionResolver:

public class MyExceptionResolver extends DefaultHandlerExceptionResolver { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    @Override 
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { 
    logger.warn("An Exception has occured in the application", exception); 


    logger.info("exception thrown " + exception.getMessage()); 
    if (exception instanceof NoSuchRequestHandlingMethodException) { 
     return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler); 
    } 

    ... 
    return mav; 
    } 

    public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, 
     HttpServletRequest request, 
     HttpServletResponse response, 
     Object handler){ 

    logger.info("Handle my exception!!!"); 

    ModelAndView mav = new ModelAndView(); 
    boolean isJSON = request.getHeader("Accept").equals("application/json"); 

    if(isJSON){ 

     ... 
    }else{ 
     ... 
    } 

    return mav; 
    } 
} 

Đoạn mã trên vẫn không có tác dụng.

Bất kỳ ý tưởng nào khác?

Trả lời

3

Theo Juergen Hoeller từ mùa xuân, không thể thực hiện được với HandlerExceptionResolver vì nó chỉ hoạt động cho lập bản đồ phụ, ví dụ:

bạn có một bộ điều khiển được ánh xạ tới /account/** và truy cập một phương thức từ nơi mà không có ánh xạ nào tồn tại như /acount/notExists so với bản đồ sẽ hoạt động.

tôi sẽ mở một JIRA cải thiện vé cho chức năng này

EDIT:

JIRA vé về vấn đề này

https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648

2

handleNoSuchRequestHandlingMethod không phải là một phần của giao diện HandlerExceptionResolver, vì vậy, việc khai báo phương thức của tên đó sẽ không làm gì cả. Đó là một phương pháp bảo vệ cụ thể để DefaultHandlerExceptionResolver, và được gọi là từ phương pháp resolveException của nó (mà phần của giao diện):

if (ex instanceof NoSuchRequestHandlingMethodException) { 
    return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, handler); 
} 

Để tái tạo các chức năng tương tự, bạn có thể subclass DefaultHandlerExceptionResolver và ghi đè lên các phương pháp bạn cần hoặc bạn cần thêm một trường hợp trong phương thức resolveException xử lý NoSuchRequestHandlingMethodException.

+0

tôi cập nhật mã của tôi. Ý bạn là nó như thế này? –

+1

Tôi cũng đã thử nó với kế thừa từ '' DefaultHandlerExceptionResolver'', nhưng trong trường hợp 404 'doResolveException'' không bao giờ được gọi ... tôi có hiểu nhầm bạn không? –

Các vấn đề liên quan