所谓的全局异常捕获指的是系统的最外层Controller层的异常捕获。febs-common-core包中的BaseExceptionHandler类中定义了系统中几种常见的异常处理: ```java @Slf4j public class BaseExceptionHandler { @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public FebsResponse handleException(Exception e) { String message = FebsUtil.containChinese(e.getMessage()) ? e.getMessage() : "系统内部异常"; log.error(message, e); return new FebsResponse().message(message); } @ExceptionHandler(value = FebsException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public FebsResponse handleFebsException(FebsException e) { log.error("FEBS系统异常", e); return new FebsResponse().message(e.getMessage()); } /** * 统一处理请求参数校验(实体对象传参) * * @param e BindException * @return FebsResponse */ @ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public FebsResponse handleBindException(BindException e) { StringBuilder message = new StringBuilder(); List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); for (FieldError error : fieldErrors) { message.append(error.getField()).append(error.getDefaultMessage()).append(","); } message = new StringBuilder(message.substring(0, message.length() - 1)); log.error(message.toString()); return new FebsResponse().message(message.toString()); } /** * 统一处理请求参数校验(普通传参) * * @param e ConstraintViolationException * @return FebsResponse */ @ExceptionHandler(value = ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public FebsResponse handleConstraintViolationException(ConstraintViolationException e) { StringBuilder message = new StringBuilder(); Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); for (ConstraintViolation<?> violation : violations) { Path path = violation.getPropertyPath(); String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); message.append(pathArr[1]).append(violation.getMessage()).append(","); } message = new StringBuilder(message.substring(0, message.length() - 1)); log.error(message.toString()); return new FebsResponse().message(message.toString()); } /** * 统一处理请求参数校验(json) * * @param e ConstraintViolationException * @return FebsResponse */ @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public FebsResponse handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) { StringBuilder message = new StringBuilder(); for (FieldError error : e.getBindingResult().getFieldErrors()) { message.append(error.getField()).append(error.getDefaultMessage()).append(","); } message = new StringBuilder(message.substring(0, message.length() - 1)); log.error(message.toString()); return new FebsResponse().message(message.toString()); } @ExceptionHandler(value = FileDownloadException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public void handleFileDownloadException(FileDownloadException e) { log.error("FileDownloadException", e); } @ExceptionHandler(value = AccessDeniedException.class) @ResponseStatus(HttpStatus.FORBIDDEN) public FebsResponse handleAccessDeniedException() { return new FebsResponse().message("没有权限访问该资源"); } @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public FebsResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) { String message = "该方法不支持" + StringUtils.substringBetween(e.getMessage(), "'", "'") + "媒体类型"; log.error(message); return new FebsResponse().message(message); } @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public FebsResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { String message = "该方法不支持" + StringUtils.substringBetween(e.getMessage(), "'", "'") + "请求方法"; log.error(message); return new FebsResponse().message(message); } } ``` 微服务子系统只需要继承该类即可: ```java @RestControllerAdvice @Order(value = Ordered.HIGHEST_PRECEDENCE) public class GlobalExceptionHandler extends BaseExceptionHandler { } ``` 后续微服务子系统中有新的异常需要单独捕获处理的话,只需要在`GlobalExceptionHandler`中定义即可。