springboot中异常全局统一处理

亦凉 2022-03-17 09:16 381阅读 0赞

个人博客

日常后端业务开发中,在提供接口服务时会遇到各种异常处理,通常涉及到参数校验异常、自定义异常以及一些不可预知的异常等等。下面就来说一下在springboot中如何在接口层进行全局性的异常处理。

全局异常处理

全局异常处理借用springmvc中的@ControllerAdvice注解来实现,当然在springboot中我们就用@RestControllerAdvice(内部包含@ControllerAdvice和@ResponseBody的特性)。此注解用来监听controller层抛出的异常,以至于让我们能进一步对异常做处理。

下面给出一个全局异常处理案例代码,涉及的有validaton注解校验的异常处理,自定义异常的处理,未知异常的处理。

注意:异常的handler域是由小至大的,所以有先后顺序。

  1. package com.lazycece.sbac.exception.controller;
  2. import com.lazycece.sbac.exception.exception.AbstractGlobalException;
  3. import com.lazycece.sbac.exception.response.ResCode;
  4. import com.lazycece.sbac.exception.response.ResMsg;
  5. import com.lazycece.sbac.exception.response.ResponseData;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.validation.BindException;
  8. import org.springframework.validation.BindingResult;
  9. import org.springframework.web.bind.MethodArgumentNotValidException;
  10. import org.springframework.web.bind.annotation.ExceptionHandler;
  11. import org.springframework.web.bind.annotation.RestControllerAdvice;
  12. import javax.servlet.ServletException;
  13. /** * @author lazycece * @date 2019/02/23 */
  14. @RestControllerAdvice
  15. @Slf4j
  16. public class GlobalExceptionHandler {
  17. /** * 参数校验错误异常 * * @param e MethodArgumentNotValidException|BindException * @return ResponseData */
  18. @ExceptionHandler(value = { BindException.class, MethodArgumentNotValidException.class})
  19. public ResponseData bindExceptionHandler(Exception e) {
  20. BindingResult bindingResult;
  21. if (e instanceof BindException) {
  22. bindingResult = ((BindException) e).getBindingResult();
  23. } else {
  24. bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
  25. }
  26. StringBuilder stringBuilder = new StringBuilder();
  27. bindingResult.getAllErrors().forEach(
  28. objectError ->
  29. stringBuilder.append(",").append(objectError.getDefaultMessage())
  30. );
  31. String errorMessage = stringBuilder.toString();
  32. errorMessage = errorMessage.substring(1, errorMessage.length());
  33. return ResponseData.builder().code(ResCode.FAIL).message(errorMessage).build();
  34. }
  35. /** * 捕获自定义的统一全局异常 * * @param e AbstractGlobalException * @return ResponseData */
  36. @ExceptionHandler(value = AbstractGlobalException.class)
  37. public ResponseData customExceptionHandler(AbstractGlobalException e) {
  38. return ResponseData.builder().code(e.getCode()).message(e.getMessage()).build();
  39. }
  40. /** * 捕获未知异常 * * @param e Exception * @return ResponseData */
  41. @ExceptionHandler(value = Exception.class)
  42. public ResponseData commonExceptionHandler(Exception e) throws ServletException {
  43. if (e instanceof ServletException) {
  44. throw (ServletException) e;
  45. }
  46. log.error("server inner error: {}", e);
  47. return ResponseData.builder().code(ResCode.FAIL).message(ResMsg.SERVER_INNER_ERROR).build();
  48. }
  49. }

案例源码

案例源码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-exception

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot异常全局统一处理

    [个人博客][Link 1] 日常后端业务开发中,在提供接口服务时会遇到各种异常处理,通常涉及到参数校验异常、自定义异常以及一些不可预知的异常等等。下面就来说一下在sprin