用好spring mvc validator可以简化代码

时间:2024-05-19 08:05:07

用好spring mvc validator可以简化代码

表单的数据检验对一个程序来讲非常重要,因为对于客户端的数据不能完全信任,常规的检验类型有:

  • 参数为空,根据不同的业务规定要求表单项是必填项
  • 参数值的有效性,比如产品的价格,一定不能是负数
  • 多个表单项组合检验,比如在注册时密码与确认密码必须相同
  • 参数值的数据范围,常见的是一些状态值,或者叫枚举值,如果传递的参数超出已经定义的枚举那么也是无意义的

上面的这些检验基本上都是纯数据方面的,还不算具体的业务数据检验,下面是一些强业务相关的数据检验

  • 根据产品ID,去检验ID是否真实存在
  • 注册用户时,需要检验用户名的唯一性
  • ....

根据上面的需求,如果我们将这些检验的逻辑全部与业务逻辑耦合在一起,那么我们的程序逻辑将会变得冗长而且不便于代码复用,下面的代码就是耦合性强的一种体现:

           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来解耦表单数据的检验逻辑,可以将上述的代码从具体的业务代码的抽离出去。

用好spring mvc validator可以简化代码
Hibernate validator,它是JSR-303的一种具体实现。它是基于注解形式的,我们看一下它原生支持的一些注解。

注解 说明
@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之间

基础数据类型的使用示例

@NotNull(message = "基础数量不能为空")
@Min(value = 0,message = "基础数量不合法")
private Integer baseQty;

嵌套检验,如果一个对象中包含子对象(非基础数据类型)需要在属性上增加@Valid注解。

   @Valid
@NotNull(message = "价格策略内容不能为空")
private List<ProductPricePolicyItem> policyItems;

除了原生提供的注解外,我们还可以自定义一些限制性的检验类型,比如上面提到的多个属性之间的联合检验。该注解需要使用@Constraint标注,这里我编写了一个用于针对两个属性之间的数据检验的规则,它支持两个属性之间的如下操作符,而且可以设置多组属性对。

  • ==
  • >
  • >=
  • <
  • <=

创建注解

  • 通过@Constraint指定检验的实现类CrossFieldMatchValidator
  • 增加两个属性名称字段,用于后续的检验
  • 增加一个注解的List,用来支持一个对象中检验多组属性对。比如即需要检验最大数量与最小数量,也需要检验密码与确认密码
@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;
}
}

用好spring mvc validator可以简化代码
需要在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>

用好spring mvc validator可以简化代码

经过上面在对象属性上的数据检验注解,我们将大部分的数据检验逻辑从业务逻辑中转移出去,不光是精简了代码还使得原本复杂的代码变得简单清晰,代码的重复利用率也增强了。

本文引用:

http://*.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303