繁体   English   中英

如何在Spring MVC中处理异常?

[英]How to handle exceptions in Spring MVC?

我在一个基于Spring MVC的项目中工作,并且通过扩展DefaultHandlerExceptionResolver来根据异常类型重定向到错误页面,从而开发了ExceptionResolver 它适用于在外观,服务,DAO层引发的异常。

但是它不适用于Servlet过滤器中引发的任何异常。 为此应该做出什么改变?

下面是我的handlerExceptionResolver

public ModelAndView doResolveException(final HttpServletRequest request, final HttpServletResponse response, final Object obj,
        final Exception exception){
    ModelAndView modelAndView = super.doResolveException(request, response, obj, exception);

    modelAndView = Objects.nonNull(modelAndView) ? modelAndView : new ModelAndView();
    final String url = Config.getParameter(EXCEPTION_HANDLER_URL);

    modelAndView.setViewName(url);
    final FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
    outputFlashMap.put(ERROR_DETAILS, exception);

    if (exception instanceof BusinessExecutionException)
    {
        return handleBusinessExecutionExceptionMethod((BusinessExecutionException) exception, outputFlashMap, modelAndView);

    }
    else if (exception instanceof IntegrationExecutionException)
    {
        return handleIntegrationExecutionExceptionMethod((IntegrationExecutionException) exception, outputFlashMap,
                modelAndView);
    }
    else if (exception instanceof DataAccessObjectExecutionException)
    {
        return handleDAOExecutionExceptionMethod((DataAccessObjectExecutionException) exception, outputFlashMap, modelAndView);
    }

    return handleMiscException(exception, outputFlashMap, modelAndView);

}

使用Spring异常处理程序: https : //spring.io/blog/2013/11/01/exception-handling-in-spring-mvc方便-您可以“捕获”有关类型和HTTP状态的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM