@Slf4j@RestControllerAdvicepublicclassGlobalExceptionHandler{/**
* 全局异常.
*
* @param e the e
* @return R
*/@ExceptionHandler(Exception.class)@ResponseBodypublicRespexception(Exception e){StackTraceElement el = e.getStackTrace()[0];if(e.getCause()instanceofInvalidFormatException|| e
.getCause()instanceofMismatchedInputException){//log.error("json数据序列化格式异常 ex={}", e.getMessage(), e);LogUtil.error(el.getClassName(),el.getMethodName(),"json数据序列化格式异常",e);returnResp.failed("json数据序列化格式异常,请检查参数类型。");}elseif(e.getMessage().contains("Duplicate entry")){LogUtil.error(el.getClassName(),el.getMethodName(),"保存唯一索引异常信息",e);returnResp.failed("数据库唯一索引异常");}elseif(e.getMessage().contains("Data too long for column")){LogUtil.error(el.getClassName(),el.getMethodName(),"字段数据太长",e);returnResp.failed("字段数据太长,写入数据库失败");}elseif(e.getCause()instanceofSQLException){LogUtil.error(el.getClassName(),el.getMethodName(),"sql异常",e);returnResp.failed("sql数据库异常");}else{LogUtil.error(el.getClassName(),el.getMethodName(),e.getMessage(),e);returnResp.failed(e.getMessage());}}/**
* 数字格式转化异常
*/@ExceptionHandler(NullPointerException.class)@ResponseBodypublicResp<String>numberFormatException(NullPointerException e){StackTraceElement el = e.getStackTrace()[0];LogUtil.error(el.getClassName(),el.getMethodName(),"空指针异常",e);returnResp.failed("空指针异常");}/**
* 数字格式转化异常
*/@ExceptionHandler(NumberFormatException.class)@ResponseBodypublicResp<String>numberFormatException(NumberFormatException e){StackTraceElement el = e.getStackTrace()[0];LogUtil.error(el.getClassName(),el.getMethodName(),"数字类型转换异常",e);returnResp.failed("数字转换异常");}/**
* 业务异常.
*
* @param e the e
* @return Resp
*/@ExceptionHandler(BusinessException.class)@ResponseBodypublicResp<String>businessException(BusinessException e){StackTraceElement el = e.getStackTrace()[0];LogUtil.error(el.getClassName(),el.getMethodName(),e.getMessage(),e);if(e.getCode()!=-100){returnResp.failed(e.getErrorEnum());}returnResp.failed(e.getMessage());}@ExceptionHandler(value =MethodArgumentNotValidException.class)publicResp<String>methodArgumentNotValidHandler(MethodArgumentNotValidException e){List<FieldError> list = e.getBindingResult().getFieldErrors();String msg =executeValidException(e, list);StackTraceElement el = e.getStackTrace()[0];LogUtil.error(el.getClassName(),el.getMethodName(),msg,e);returnResp.failed(CodeRs.PARAM_ERROR.getCode(),msg);}@ExceptionHandler(value =BindException.class)publicResp<String>methodArgumentNotValidHandler(BindException e){List<FieldError> list = e.getBindingResult().getFieldErrors();String msg =executeValidException(e, list);StackTraceElement el = e.getStackTrace()[0];LogUtil.error(el.getClassName(),el.getMethodName(),msg,e);returnResp.failed(CodeRs.PARAM_ERROR.getCode(),msg);}privateStringexecuteValidException(Exception e,List<FieldError> list){//按需重新封装需要返回的错误信息StringBuilder sb =newStringBuilder();//解析原错误信息,封装后返回,此处返回非法的字段名称,错误信息for(FieldError error : list){
sb.append("字段").append(error.getField()).append(",").append(error.getDefaultMessage()).append(";");}return"参数验证失败:"+ sb.toString();}}