韦二铭 5 år sedan
förälder
incheckning
176e7f54a7

+ 22 - 2
base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/web/handler/BaseExceptionHandler.java

@@ -6,6 +6,7 @@ import com.wei.base.springframework.constant.vo.RestfulVO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.validation.BindException;
 import org.springframework.validation.ObjectError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.MissingServletRequestParameterException;
@@ -66,8 +67,8 @@ public class BaseExceptionHandler {
         List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
         StringBuilder stringBuilder = new StringBuilder();
 
-        allErrors.forEach(vo -> {
-            stringBuilder.append(vo.getDefaultMessage().toString() + ";");
+        allErrors.forEach(error -> {
+            stringBuilder.append(error.getDefaultMessage().toString() + ";");
         });
         return new RestfulVO<>(RestfulEnum.FAIL, stringBuilder.toString());
     }
@@ -99,6 +100,25 @@ public class BaseExceptionHandler {
     }
 
     /**
+     * 封装bind抛出的错误
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(BindException.class)
+    @ResponseBody
+    public RestfulVO<String> bindException(BindException e) {
+        log.error(e.getMessage(), e);
+        List<ObjectError> allErrors = e.getAllErrors();
+        StringBuilder stringBuilder = new StringBuilder();
+
+        allErrors.forEach(error -> {
+            stringBuilder.append(error.getDefaultMessage() + ";");
+        });
+        return new RestfulVO<>(RestfulEnum.FAIL, stringBuilder.toString());
+    }
+
+    /**
      * 将错误全部拦截封装放回 如果有其他异常需要自己处理的需要在定义错误拦截器
      *
      * @param e

+ 0 - 1
base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/web/handler/ResponseHandler.java

@@ -3,7 +3,6 @@ package com.wei.base.springframework.core.web.handler;
 import com.wei.base.springframework.constant.vo.RestfulVO;
 import com.wei.base.springframework.core.web.config.ResponseHandlerConfigure;
 import com.wei.base.springframework.util.RegexUtil;
-import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.MethodParameter;