【问题标题】:Spring Boot - Pass Exception object from ResponseEntityExceptionHandler to HandlerInterceptor?Spring Boot - 将异常对象从 ResponseEntityExceptionHandler 传递给 HandlerInterceptor?
【发布时间】:2019-11-06 13:18:46
【问题描述】:

我正在研究 Spring Boot 示例并实现 GlobalExceptionHandler 并尝试以 JSON 格式打印所有错误消息 - 这是我的自定义方法。

另外,我有ExceptionHandler 在那里我捕获了所有异常。但是有什么方法可以将异常对象从ResponseEntityExceptionHandler 传递给HandlerInterceptor

HandlerInterceptor:

@Slf4j
public class GlobalExceptionHandler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        ............
        .............
        ..............
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {

        ServletRequestAttributes attributes = (ServletRequestAttributes) request.getAttribute(REQUEST_ATTRIBUTES);
        ServletRequestAttributes threadAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();    
        ............
        .............
        ..............
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        if(ex != null) {
            printJsonReq(request, response);
        }
    }
}

异常处理程序:

@ControllerAdvice
@Slf4j
public class ExceptionHandler extends ResponseEntityExceptionHandler{

    @ExceptionHandler({ResponseStatusException.class})
    protected ResponseEntity<Object> handleResStatusException(Exception e, WebRequest request, HttpServletRequest httpRequest) {
        ResponseStatusException be = (ResponseStatusException) e;

        ErrorResource error = ErrorResource.builder().code(AppConst.BAD_REQUEST)
                .message(ExceptionUtils.getDetails(e.getCause())).build();

        return handleExceptionInternal(e, error, getHeaders(), HttpStatus.BAD_REQUEST, request);
    }

    .........
    ..........
    .........
}

【问题讨论】:

    标签: spring spring-boot exceptionhandler handlerinterceptor


    【解决方案1】:

    您可以将其设置为ExceptionHandler 类中的请求属性(如果您需要它只是为了确保您要打印日志,那么您可以传递布尔参数以不加载您的请求对象,而不是传递异常对象)

    request.setAttribute("exception", e);
    

    在你的 HandlerInterceptor 中使用它

    if(ex != null || request.getAttribute("exception") != null) {
       printJsonReq(request, response);
    }
    

    【讨论】:

    • 太棒了!我一直在寻找这个 2 天。我想将异常记录到拦截器中,但是当使用 ExceptionHandler 时,ExceptionHandler 会使异常静音。这是一个聪明的解决方案,我们可以访问双方的请求对象,因此可以通过请求发送数据。这解决了我的问题!谢谢!
    【解决方案2】:

    您可以使用WebMvcConfigurerAdapter配置拦截器

    17.15.3 Configuring Interceptors

    您可以将 HandlerInterceptors 或 WebRequestInterceptors 配置为应用于所有传入请求或限制为特定的 URL 路径模式。

    Java中注册拦截器的例子:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(new GlobalExceptionHandler());
    
          }
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多