spring boot 项目在Filter中抛出异常处理方法

spring boot 项目在Filter中抛出异常,使用@ControllerAdvice+@ExceptionHandler无法处理,处理方法如下:

自己创建Controller继承BasicErrorController,具体实现

@RestController
@Api(value = "filter错误处理", description = "filter错误处理")
public class ErrorController extends BasicErrorController {

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

    @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity> error(HttpServletRequest request) {
        Map body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
       //自定义的错误信息类
       //status.value():错误代码,
       //body.get("message").toString()错误信息
        R ret = R.error(status.value(), body.get("message").toString());
        //TokenException Filter抛出的自定义错误类
        if (!Strings.isNullOrEmpty((String) body.get("exception")) && body.get("exception").equals(TokenException.class.getName())) {
            body.put("status", HttpStatus.FORBIDDEN.value());
            status = HttpStatus.FORBIDDEN;
        }
        return new ResponseEntity>(ret, status);
    }

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

}

你可能感兴趣的:(spring,boot,spring,boot,Filter异常)