设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 系统 > 正文

你以为Spring Boot统一异常处理能拦截所有的问题?

发布时间:2021-06-01 11:18 所属栏目:52 来源:互联网
导读:我们可以从上面的几个属性中获取异常的详细信息。 默认错误页面 通常Spring Boot出现异常默认会跳转到/error进行处理,而/error的相关逻辑则是由BasicErrorContr

@RequestMapping("${server.error.path:${error.path:/error}}") 

public class BasicErrorController extends AbstractErrorController { 

    //返回错误页面 

  @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 

 public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 

  HttpStatus status = getStatus(request); 

  Map<String, Object> model = Collections 

    .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML))); 

  response.setStatus(status.value()); 

  ModelAndView modelAndView = resolveErrorView(request, response, status, model); 

  return (modelAndView != null) ? modelAndView : new ModelAndView("error", model); 

 } 

    // 返回json 

 @RequestMapping 

 public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { 

  HttpStatus status = getStatus(request); 

  if (status == HttpStatus.NO_CONTENT) { 

   return new ResponseEntity<>(status); 

  } 

  Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)); 

  return new ResponseEntity<>(body, status); 

 }   

// 其它省略 

而对应的配置:

@Bean 

@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) 

public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, 

      ObjectProvider<ErrorViewResolver> errorViewResolvers) { 

   return new BasicErrorController(errorAttributes, this.serverProperties.getError(), 

         errorViewResolvers.orderedStream().collect(Collectors.toList())); 

所以我们只需要重新实现一个ErrorController并注入Spring IoC就可以替代默认的处理机制。而且我们可以很清晰的发现这个BasicErrorController不但是ErrorController的实现而且是一个控制器,如果我们让控制器的方法抛异常,肯定可以被自定义的统一异常处理。所以我对BasicErrorController进行了改造:

@Controller 

@RequestMapping("${server.error.path:${error.path:/error}}") 

public class ExceptionController extends AbstractErrorController { 

 

 

    public ExceptionController(ErrorAttributes errorAttributes) { 

        super(errorAttributes); 

    } 

 

 

    @Override 

    @Deprecated 

    public String getErrorPath() { 

        return null; 

    } 

 

    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 

    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 

        throw new RuntimeException(getErrorMessage(request)); 

    } 

 

    @RequestMapping 

    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { 

        throw new RuntimeException(getErrorMessage(request)); 

    } 

 

    private String getErrorMessage(HttpServletRequest request) { 

        Object code = request.getAttribute("javax.servlet.error.status_code"); 

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读