栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot 全局异常处理

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 全局异常处理

SpringBoot 全局异常处理
@Slf4j
@RestControllerAdvice
public class ExceptionController {

    
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiMessage methodArgumentNotValidHandler(MethodArgumentNotValidException ex) {
 //按需重新封装需要返回的错误信息
 List invalidArguments = new ArrayList<>();
 //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
 ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage())));
 return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments);
    }
  
    @ExceptionHandler(value = BindException.class)
    public ApiMessage bindExceptionHandler(BindException ex) {
 //按需重新封装需要返回的错误信息
 List invalidArguments = new ArrayList<>();
 //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
 ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage())));
 return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments);
    }

    
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ApiMessage httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
 log.error("HTTP请求方式不正确:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public ApiMessage missingServletRequestParameterException(MissingServletRequestParameterException ex) {
 log.error("请求参数不全:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = TypeMismatchException.class)
    public ApiMessage typeMismatchException(TypeMismatchException ex) {
 log.error("请求参数类型不正确:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = DataFormatException.class)
    public ApiMessage dataFormatException(DataFormatException ex) {
 log.error("数据格式不正确:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ApiMessage illegalArgumentException(IllegalArgumentException ex) {
 log.error("非法输入或断言错误:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = ConstraintViolationException.class)
    public ApiMessage constraintViolationException(ConstraintViolationException ex) {
 log.error("请求参数错误:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(value = DataAccessException.class)
    public ApiMessage dataDoException(DataAccessException ex) {
 log.error("操作数据库出现异常:【{}】", ex.getMessage());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(Exception.class)
    public ApiMessage apiExceptionHandler(Exception ex) {
 //只打印15行的错误堆栈
 int count = 1;
 StringBuilder sb = new StringBuilder();
 for (StackTraceElement stackTraceElement : ex.getStackTrace()) {
     sb.append(stackTraceElement.toString());
//     if (count++ >= 30) {
//  break;
//     }
     sb.append("n");
 }
 log.error("系统异常:【{}】", sb.toString());
 return new ApiMessage<>(ex);
    }

    
    @ExceptionHandler(ApiException.class)
    public ApiMessage apiException(ApiException apiException) {
 return new ApiMessage<>(apiException);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode(callSuper = false)
    static class ArgumentInvalid {
 
 private String field;
 
 private Object rejectedValue;
 
 private String defaultMessage;
    }
}

赵小胖个人博客

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号