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

副标题#e# @RequestMapping(${server.error.path:${error.path:/error}}) publicclassBasicErrorControllerextendsAbstractErrorController{ //返回错误页面 @RequestMapping(produces=MediaType.TEXT_HTML_VALUE) publicModelAndViewerrorHtml(HttpServlet

副标题#e#

@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"); 

#p#副标题#e#

        Object exceptionType = request.getAttribute("javax.servlet.error.exception_type"); 

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

        Object path = request.getAttribute("javax.servlet.error.request_uri"); 

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

 

        return String.format("code: %s,exceptionType: %s,message: %s,path: %s,exception: %s", 

                code, exceptionType, message, path, exception); 

    } 

直接抛异常,简单省力!凡是这里捕捉的到的异常大部分还没有经过Controller,我们通过ExceptionController中继也让这些异常被统一处理,保证整个应用的异常处理对外保持一个统一的门面。

关于作者: dawei

【声明】:石家庄站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐