From 5d2318f72d1c5a7a8213af653ad36bcd3d8b7415 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 5 Nov 2019 16:28:58 +0800 Subject: [PATCH] =?UTF-8?q?JSR-303=E6=94=AF=E6=8C=81=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../param/ServiceParamValidator.java | 25 +++++++++++++------ .../gitee/sop/servercommon/ValidatorTest.java | 14 +++++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java index 3fd9c255..109492ad 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java @@ -11,6 +11,7 @@ import org.springframework.util.ReflectionUtils; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.ValidatorFactory; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -57,23 +58,31 @@ public class ServiceParamValidator implements ParamValidator { private List listObjectField(Object object) { List ret = new ArrayList<>(); ReflectionUtils.doWithFields(object.getClass(), field -> { - if (isCustomPackage(field.getType())) { - ReflectionUtils.makeAccessible(field); - ret.add(field.get(object)); - } - }); + ReflectionUtils.makeAccessible(field); + ret.add(field.get(object)); + }, this::isMatchField); return ret; } /** - * 字段类型所在package是否是自定义包 - * @param fieldType 指定的类型 + * 匹配校验字段。 + * + * 1. 不为基本类型; + * 2. 不为java自带的类型; + * 3. 不为枚举 + * @param field field * @return true,是自定义的 */ - private boolean isCustomPackage(Class fieldType) { + private boolean isMatchField(Field field) { + Class fieldType = field.getType(); if (fieldType.isPrimitive()) { return false; } + Class declaringClass = field.getDeclaringClass(); + boolean isEnum = declaringClass == field.getType() && declaringClass.isEnum(); + if (isEnum) { + return false; + } Package aPackage = fieldType.getPackage(); if (aPackage == null) { return false; diff --git a/sop-common/sop-service-common/src/test/java/com/gitee/sop/servercommon/ValidatorTest.java b/sop-common/sop-service-common/src/test/java/com/gitee/sop/servercommon/ValidatorTest.java index 963d931a..e3e27afb 100644 --- a/sop-common/sop-service-common/src/test/java/com/gitee/sop/servercommon/ValidatorTest.java +++ b/sop-common/sop-service-common/src/test/java/com/gitee/sop/servercommon/ValidatorTest.java @@ -31,8 +31,8 @@ public class ValidatorTest extends TestCase { public void testField() { - Manager manager = new Manager("Jim", 22); - Store store = new Store("仓库A", manager); + Manager manager = new Manager("Jim", 22, Type.TWO); + Store store = new Store("仓库A", manager, Type.ONE); Goods goods = new Goods("Apple", new BigDecimal(50000), store); serviceParamValidator.validateBizParam(goods); } @@ -75,6 +75,9 @@ public class ValidatorTest extends TestCase { @NotNull(message = "管理员不能为空") private Manager manager; + + @NotNull(message = "Store.type不能为空") + private Type type; } @Data @@ -84,6 +87,13 @@ public class ValidatorTest extends TestCase { private String name; private int age; + + @NotNull(message = "Manager.type不能为空") + private Type type; + } + + enum Type { + ONE,TWO } }