当前位置:   article > 正文

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

springboot 处理 filter 中抛出的异常

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

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

  1. @RestController
  2. @Api(value = "filter错误处理", description = "filter错误处理")
  3. public class ErrorController extends BasicErrorController {
  4. public ErrorController() {
  5. super(new DefaultErrorAttributes(), new ErrorProperties());
  6. }
  7. @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
  8. public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
  9. Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
  10. HttpStatus status = getStatus(request);
  11. //自定义的错误信息类
  12. //status.value():错误代码,
  13. //body.get("message").toString()错误信息
  14. R ret = R.error(status.value(), body.get("message").toString());
  15. //TokenException Filter抛出的自定义错误类
  16. if (!Strings.isNullOrEmpty((String) body.get("exception")) && body.get("exception").equals(TokenException.class.getName())) {
  17. body.put("status", HttpStatus.FORBIDDEN.value());
  18. status = HttpStatus.FORBIDDEN;
  19. }
  20. return new ResponseEntity<Map<String, Object>>(ret, status);
  21. }
  22. @Override
  23. public String getErrorPath() {
  24. return "error/error";
  25. }
  26. }
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/AI算法诗人/article/detail/62623
推荐阅读
相关标签
  

闽ICP备14008679号