所谓的全局异常捕获指的是系统的最外层Controller层的异常捕获。qingfeng-common-core包中的BaseExceptionHandler类中定义了系统中几种常见的异常处理:

    1. package com.qingfeng.handler;
    2. import com.qingfeng.entity.MyResponse;
    3. import com.qingfeng.exception.AuthException;
    4. import com.qingfeng.exception.MyException;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.apache.commons.lang3.StringUtils;
    7. import org.springframework.http.HttpStatus;
    8. import org.springframework.security.access.AccessDeniedException;
    9. import org.springframework.validation.BindException;
    10. import org.springframework.validation.FieldError;
    11. import org.springframework.web.bind.annotation.ExceptionHandler;
    12. import org.springframework.web.bind.annotation.ResponseStatus;
    13. import javax.validation.ConstraintViolation;
    14. import javax.validation.ConstraintViolationException;
    15. import javax.validation.Path;
    16. import java.util.List;
    17. import java.util.Set;
    18. /**
    19. * @author Administrator
    20. * @title: BaseExceptionHandler
    21. * @projectName qingfeng-cloud
    22. * @description: TODO
    23. * @date 2021/2/21 00210:53
    24. */
    25. @Slf4j
    26. public class BaseExceptionHandler {
    27. @ExceptionHandler(value = Exception.class)
    28. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    29. public MyResponse handleException(Exception e) {
    30. log.error("系统内部异常,异常信息", e);
    31. return new MyResponse().message("系统内部异常");
    32. }
    33. @ExceptionHandler(value = AuthException.class)
    34. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    35. public MyResponse handleQingfengAuthException(AuthException e) {
    36. log.error("系统错误", e);
    37. return new MyResponse().message(e.getMessage());
    38. }
    39. @ExceptionHandler(value = AccessDeniedException.class)
    40. @ResponseStatus(HttpStatus.FORBIDDEN)
    41. public MyResponse handleAccessDeniedException(){
    42. return new MyResponse().message("没有权限访问该资源");
    43. }
    44. /**
    45. * @title: 自定义异常
    46. * @description: TODO
    47. * @author: Administrator
    48. * @date: 2021/2/21 0021 22:22
    49. */
    50. @ExceptionHandler(value = MyException.class)
    51. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    52. public MyResponse handleException(MyException e) {
    53. log.error("系统错误", e);
    54. return new MyResponse().message(e.getMessage());
    55. }
    56. /**
    57. * @title: BaseExceptionHandler
    58. * @projectName: BaseExceptionHandler
    59. * @description: 统一处理请求参数校验(普通传参)
    60. * @author: Administrator
    61. * @date: 2021/2/23 0023 21:58
    62. */
    63. @ExceptionHandler(value = ConstraintViolationException.class)
    64. @ResponseStatus(HttpStatus.BAD_REQUEST)
    65. public MyResponse handleConstraintViolationException(ConstraintViolationException e) {
    66. StringBuilder message = new StringBuilder();
    67. Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
    68. for (ConstraintViolation<?> violation : violations) {
    69. Path path = violation.getPropertyPath();
    70. String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), ".");
    71. message.append(pathArr[1]).append(violation.getMessage()).append(",");
    72. }
    73. message = new StringBuilder(message.substring(0, message.length() - 1));
    74. return new MyResponse().message(message.toString());
    75. }
    76. /**
    77. * @title: BaseExceptionHandler
    78. * @projectName: BaseExceptionHandler
    79. * @description: 统一处理请求参数校验(实体对象传参)
    80. * @author: Administrator
    81. * @date: 2021/2/23 0023 21:59
    82. */
    83. @ExceptionHandler(BindException.class)
    84. @ResponseStatus(HttpStatus.BAD_REQUEST)
    85. public MyResponse handleBindException(BindException e) {
    86. StringBuilder message = new StringBuilder();
    87. List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
    88. for (FieldError error : fieldErrors) {
    89. message.append(error.getField()).append(error.getDefaultMessage()).append(",");
    90. }
    91. message = new StringBuilder(message.substring(0, message.length() - 1));
    92. return new MyResponse().message(message.toString());
    93. }
    94. }

    微服务子系统只需要继承该类即可:

    1. package com.qingfeng.handler;
    2. import org.springframework.core.Ordered;
    3. import org.springframework.core.annotation.Order;
    4. import org.springframework.web.bind.annotation.RestControllerAdvice;
    5. /**
    6. * @ProjectName GlobalExceptionHandler
    7. * @author Administrator
    8. * @version 1.0.0
    9. * @Description TODO
    10. * @createTime 2021/4/19 0019 13:01
    11. */
    12. @RestControllerAdvice
    13. @Order(value = Ordered.HIGHEST_PRECEDENCE)
    14. public class GlobalExceptionHandler extends BaseExceptionHandler {
    15. }

    后续微服务子系统中有新的异常需要单独捕获处理的话,只需要在GlobalExceptionHandler中定义即可。