
表單的數據檢驗對一個程序來講非常重要,因為對於客戶端的數據不能完全信任,常規的檢驗類型有:
上面的這些檢驗基本上都是純數據方面的,還不算具體的業務數據檢驗,下面是一些強業務相關的數據檢驗
根據上面的需求,如果我們將這些檢驗的邏輯全部與業務邏輯耦合在一起,那麼我們的程序邏輯將會變得冗長而且不便於代碼復用,下面的代碼就是耦合性強的一種體現:
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來解耦表單數據的檢驗邏輯,可以將上述的代碼從具體的業務代碼的抽離出去。

Hibernate validator,它是JSR-303的一種具體實現。它是基於注解形式的,我們看一下它原生支持的一些注解。
限制字符長度必須在min到max之間
基礎數據類型的使用示例
@NotNull(message = "基礎數量不能為空")
@Min(value = 0,message = "基礎數量不合法")
private Integer baseQty;
嵌套檢驗,如果一個對象中包含子對象(非基礎數據類型)需要在屬性上增加@Valid注解。
@Valid
@NotNull(message = "價格策略內容不能為空")
private List<ProductPricePolicyItem> policyItems;
除了原生提供的注解外,我們還可以自定義一些限制性的檢驗類型,比如上面提到的多個屬性之間的聯合檢驗。該注解需要使用@Constraint標注,這裡我編寫了一個用於針對兩個屬性之間的數據檢驗的規則,它支持兩個屬性之間的如下操作符,而且可以設置多組屬性對。
創建注解
@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