如何处理Spring MVC中的异常?

时间:2022-09-11 14:41:07

I am working in a spring mvc based project and have developed a ExceptionResolver by extending DefaultHandlerExceptionResolver to redirect to error page depending on the exception type. It is working for exception raised at facade, service, DAO layer.

我正在一个基于Spring mvc的项目中工作,并通过扩展DefaultHandlerExceptionResolver以根据异常类型重定向到错误页面来开发ExceptionResolver。它适用于在门面,服务,DAO层提出的异常。

But it doesn't work for any exceptions raised in Servlet filter. What changes should be made for that?

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

Below is my handlerExceptionResolver

下面是我的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);

}

1 个解决方案

#1


0  

Use Spring exception handler: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc It is convenient - you can "catch" exception regarding type and HTTP status.

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

#1


0  

Use Spring exception handler: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc It is convenient - you can "catch" exception regarding type and HTTP status.

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