Selaa lähdekoodia

优化参数校验异常处理逻辑

Woody 2 viikkoa sitten
vanhempi
commit
c35a640c99

+ 8 - 3
framework-base/src/main/java/com/chelvc/framework/base/interceptor/GlobalExceptionInterceptor.java

@@ -126,8 +126,12 @@ public class GlobalExceptionInterceptor extends AbstractErrorController implemen
         if (ObjectUtils.isEmpty(errors)) {
             return Collections.emptyMap();
         }
-        return errors.stream().collect(Collectors.toMap(FieldError::getField,
-                e -> ObjectUtils.ifNull(e.getDefaultMessage(), StringUtils.EMPTY), (o, n) -> n));
+        return errors.stream().collect(Collectors.toMap(FieldError::getField, e -> {
+            if (Objects.isNull(e.getDefaultMessage()) || e.contains(TypeMismatchException.class)) {
+                return ApplicationContextHolder.getMessage("Parameter.Invalid");
+            }
+            return e.getDefaultMessage();
+        }, (o, n) -> n));
     }
 
     /**
@@ -141,8 +145,9 @@ public class GlobalExceptionInterceptor extends AbstractErrorController implemen
         if (ObjectUtils.isEmpty(references)) {
             return Collections.emptyMap();
         }
+        String message = ApplicationContextHolder.getMessage("Parameter.Invalid");
         return references.stream().collect(Collectors.toMap(
-                JsonMappingException.Reference::getFieldName, r -> "Invalid value"
+                JsonMappingException.Reference::getFieldName, r -> message, (o, n) -> n
         ));
     }