|
@@ -1,15 +1,21 @@
|
|
|
package com.fdkankan.common.exception;
|
|
|
|
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
import com.fdkankan.common.constant.ServerCode;
|
|
|
import com.fdkankan.common.exception.BusinessException;
|
|
|
import com.fdkankan.common.response.ResultData;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
+import org.springframework.validation.ObjectError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 全局异常处理器
|
|
@@ -29,6 +35,32 @@ public class GlobalExceptionHandler {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 校验错误拦截处理
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public ResultData validationBodyException(MethodArgumentNotValidException exception) {
|
|
|
+ BindingResult result = exception.getBindingResult();
|
|
|
+ String message = "";
|
|
|
+ if (result.hasErrors()) {
|
|
|
+ List<ObjectError> errors = result.getAllErrors();
|
|
|
+ if (errors != null) {
|
|
|
+ errors.forEach(p -> {
|
|
|
+ FieldError fieldError = (FieldError) p;
|
|
|
+ log.error("Data check failure : object{" + fieldError.getObjectName() + "},field{" + fieldError.getField() +
|
|
|
+ "},errorMessage{" + fieldError.getDefaultMessage() + "}");
|
|
|
+
|
|
|
+ });
|
|
|
+ if (errors.size() > 0) {
|
|
|
+ FieldError fieldError = (FieldError) errors.get(0);
|
|
|
+ message = fieldError.getDefaultMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultData.error(ErrorCode.PARAM_FORMAT_ERROR.code(), message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 处理业务异常
|
|
|
*/
|
|
|
@ResponseBody
|