表单的数据检验对一个程序来说很是重要,由于对于客户端的数据不能彻底信任,常规的检验类型有:git
上面的这些检验基本上都是纯数据方面的,还不算具体的业务数据检验,下面是一些强业务相关的数据检验正则表达式
根据上面的需求,若是咱们将这些检验的逻辑所有与业务逻辑耦合在一块儿,那么咱们的程序逻辑将会变得冗长并且不便于代码复用,下面的代码就是耦合性强的一种体现:spring
if (isvUserRequestDTO == null) { log.error("can not find isv request by request id, " + isvRequestId); return return_value_error(ErrorDef.FailFindIsv); } if (isvUserRequestDTO.getAuditStatus() != 1) { log.error("isv request is not audited, " + isvRequestId); return return_value_error(ErrorDef.IsvRequestNotAudited); }
咱们能够利用spring提供的validator来解耦表单数据的检验逻辑,能够将上述的代码从具体的业务代码的抽离出去。mvc
Hibernate validator,它是JSR-303的一种具体实现。它是基于注解形式的,咱们看一下它原生支持的一些注解。
ide
注解 | 说明 |
@Null | 只能为空,这个用途场景比较少 |
@NotNull | 不能为空,经常使用注解 |
@AssertFalse | 必须为false,相似于常量 |
@AssertTrue | 必须为true,相似于常量 |
@DecimalMax(value) | |
@DecimalMin(value) | |
@Digits(integer,fraction) | |
@Future | 表明是一个未来的时间 |
@Max(value) | 最大值,用于一个枚举值的数据范围控制 |
@Min(value) | 最小值,用于一个枚举值的数据范围控制 |
@Past | 表明是一个过时的时间 |
@Pattern(value) | 正则表达式,好比验证手机号,邮箱等,很是经常使用 |
@Size(max,min) | 限制字符长度必须在min到max之间this |
基础数据类型的使用示例spa
@NotNull(message = "基础数量不能为空") @Min(value = 0,message = "基础数量不合法") private Integer baseQty;
嵌套检验,若是一个对象中包含子对象(非基础数据类型)须要在属性上增长@Valid注解。hibernate
@Valid @NotNull(message = "价格策略内容不能为空") private List<ProductPricePolicyItem> policyItems;
除了原生提供的注解外,咱们还能够自定义一些限制性的检验类型,好比上面提到的多个属性之间的联合检验。该注解须要使用@Constraint标注,这里我编写了一个用于针对两个属性之间的数据检验的规则,它支持两个属性之间的以下操做符,并且能够设置多组属性对。3d
建立注解code
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = CrossFieldMatchValidator.class) @Documented public @interface CrossFieldMatch { String message() default "{constraints.crossfieldmatch}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; /** * @return The first field */ String first(); /** * @return The second field */ String second(); /** * first operator second * @return */ CrossFieldOperator operator(); /** * Defines several <code>@FieldMatch</code> annotations on the same element * * @see CrossFieldMatch */ @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RUNTIME) @Documented @interface List { CrossFieldMatch[] value(); } }
检验实现类
isValid方法,经过反射能够取到须要检验的两个字段的值以及数据类型,而后根据指定的数据操做符以及数据类型作出计算。目前这个检验只针对个人业务并不十分通用,须要根据本身的项目状况来灵活处理。
public class CrossFieldMatchValidator implements ConstraintValidator<CrossFieldMatch, Object> { private String firstFieldName; private String secondFieldName; private CrossFieldOperator operator; @Override public void initialize(final CrossFieldMatch constraintAnnotation) { firstFieldName = constraintAnnotation.first(); secondFieldName = constraintAnnotation.second(); operator=constraintAnnotation.operator(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { try { Class valueClass=value.getClass(); final Field firstField = valueClass.getDeclaredField(firstFieldName); final Field secondField = valueClass.getDeclaredField(secondFieldName); //不支持为null的字段 if(null==firstField||null==secondField){ return false; } firstField.setAccessible(true); secondField.setAccessible(true); Object firstFieldValue= firstField.get(value); Object secondFieldValue= secondField.get(value); //不支持类型不一样的字段 if(!firstFieldValue.getClass().equals(secondFieldValue.getClass())){ return false; } //整数支持 long int short //浮点数支持 double if(operator==CrossFieldOperator.EQ) { return firstFieldValue.equals(secondFieldValue); } else if(operator==CrossFieldOperator.GT){ if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) { return (Long)firstFieldValue > (Long) secondFieldValue; } else if(firstFieldValue.getClass().equals(Double.class)) { return (Double)firstFieldValue > (Double) secondFieldValue; } } else if(operator==CrossFieldOperator.GE){ if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) { return Long.valueOf(firstFieldValue.toString()) >= Long.valueOf(secondFieldValue.toString()); } else if(firstFieldValue.getClass().equals(Double.class)) { return Double.valueOf(firstFieldValue.toString()) >= Double.valueOf(secondFieldValue.toString()); } } else if(operator==CrossFieldOperator.LT){ if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) { return (Long)firstFieldValue < (Long) secondFieldValue; } else if(firstFieldValue.getClass().equals(Double.class)) { return (Double)firstFieldValue < (Double) secondFieldValue; } } else if(operator==CrossFieldOperator.LE){ if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) { return Long.valueOf(firstFieldValue.toString()) <= Long.valueOf(secondFieldValue.toString()); } else if(firstFieldValue.getClass().equals(Double.class)) { return Double.valueOf(firstFieldValue.toString()) <= Double.valueOf(secondFieldValue.toString()); } } } catch (final Exception ignore) { // ignore } return false; } }
调用示例:
@CrossFieldMatch.List({ @CrossFieldMatch(first = "minQty", second = "maxQty",operator = CrossFieldOperator.LE ,message = "最小数量必须小于等于最大数量") }) public class ProductPriceQtyRange implements Serializable{ /** * 最小数量 */ @Min(value = 0,message = "最小数量不合法") private int minQty; /** * 最大数量 */ @Min(value = 0,message = "最大数量不合法") private int maxQty; public int getMinQty() { return minQty; } public void setMinQty(int minQty) { this.minQty = minQty; } public int getMaxQty() { return maxQty; } public void setMaxQty(int maxQty) { this.maxQty = maxQty; } }
须要在mvc的配置文件中增长以下节点以启动检验
<mvc:annotation-driven validator="validator"> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <property name="validationMessageSource" ref="messageSource"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="useCodeAsDefaultMessage" value="false"/> <property name="defaultEncoding" value="UTF-8"/> </bean>
通过上面在对象属性上的数据检验注解,咱们将大部分的数据检验逻辑从业务逻辑中转移出去,不光是精简了代码还使得本来复杂的代码变得简单清晰,代码的重复利用率也加强了。
本文引用:
http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303