网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

RestControllerAdvice无法捕获filter中抛出的异常问题

本篇内容主要讲解“RestControllerAdvice无法捕获filter中抛出的异常问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“RestControllerAdvice无法捕获filter中抛出的异常问题”吧!

创新互联建站专注于网站建设,为客户提供网站制作、成都做网站、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,成都品牌网站建设,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。

搭建springboot+shiro+jwt的时候,发现RestControllerAdvice全局异常处理无法获取filter中的异常,然后找到这个老哥的文章,确实解决了我的问题:

记一次RestControllerAdvice无法拦截Filter内抛出异常

做个备忘也贴一下原文的内容

今天有同事用到Shiro使用JWT的时候在Filter里做身份验证,然后在里面catch捕获并抛出了自定义异常。我们这边是用的RestControllerAdvice做统一异常处理,然后这个异常并没有被RestControllerAdvice所拦截到

原因
请求进来 会按照 filter -> interceptor -> controllerAdvice -> aspect -> controller的顺序调用

当controller返回异常 也会按照controller -> aspect -> controllerAdvice -> interceptor -> filter来依次抛出

这种Filter发生的404、405、500错误都会到Spring默认的异常处理。如果你在配置文件配置了server.error.path的话,就会使用你配置的异常处理地址,如果没有就会使用你配置的error.path路径地址,如果还是没有,默认使用/error来作为发生异常的处理地址。如果想要替换默认的非Controller异常处理直接实现Spring提供的ErrorController接口就行了

解决方案
新建一个ErrorControllerImpl 实现ErrorController 把ErrorPath 指向error 再写一个方法把Error抛出 然后Controller全局统一异常处理RestControllerAdvice就能捕获到异常了

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Joe
 * createTime 2020/07/27 14:39
 * mail joe-code@foxmail.com
 */
@Controller
public class ErrorControllerImpl implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
  	
    public void handleError(HttpServletRequest request) throws Throwable {
        if (request.getAttribute("javax.servlet.error.exception") != null) {
            throw (Throwable) request.getAttribute("javax.servlet.error.exception");
        }
    }
}

最后,其实也是来自于:参考stackoverflow链接 https://stackoverflow.com/questions/34595605/how-to-manage-exceptions-thrown-in-filters-in-spring

 第二种方式也能解决,也记录一下:

package com.hs.demo.exception;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * GlobalException 全局异常处理没法处理过虑器中抛出的异常
 * 和执行顺序有关:filter -> interceptor -> controllerAdvice -> aspect -> controller
 * 当controller返回异常 也会按照controller -> aspect -> controllerAdvice -> interceptor -> filter来依次抛出
 * 注:使用ErrorControllerImpl来解决了,这个先注掉
 */
//@RestController
public class MyErrorController extends BasicErrorController {

    public MyErrorController() {
        super(new DefaultErrorAttributes(), new ErrorProperties());
    }

    @Override
    @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity> error(HttpServletRequest request) {
        // 设置一下信息,在application.properties中配置不生效,应该是和上面的构造方法传的有关,先这么配置,待研究
        getErrorProperties().setIncludeException(true);
        getErrorProperties().setIncludeMessage(ErrorProperties.IncludeAttribute.ALWAYS);
        getErrorProperties().setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ALWAYS);
        Map body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        HttpStatus status = getStatus(request);

        Map map = new HashMap();
        map.put("code", body.get("status"));
        map.put("message", "".equals(body.get("message")) ? "系统错误" : body.get("message"));
        return new ResponseEntity<>(map, status);
    }
}

同一个请求,使用第一种方式的截图

RestControllerAdvice无法捕获filter中抛出的异常问题

使用第二种方式的截图

RestControllerAdvice无法捕获filter中抛出的异常问题

个人感觉第一种方式更好

到此,相信大家对“RestControllerAdvice无法捕获filter中抛出的异常问题”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前名称:RestControllerAdvice无法捕获filter中抛出的异常问题
浏览路径:http://bjjierui.cn/article/ipsiep.html

其他资讯