Hibernate validation 注解 springmvc 验证 分组

2022-12-09,,,,

SpringMVC验证框架Validation特殊用法

 

1. 分组

有的时候,我们对一个实体类需要有多中验证方式,在不同的情况下使用不同验证方式,比如说对于一个实体类来的id来说,保存的时候是不需要的,对于更新时是必须的,可以如下配置:

    public class UserModel {
    @NotNull(message = "{id.empty}", groups = { First.class })
    private int id;
    @NotNull(message = "{username.empty}", groups = { First.class, Second.class })
    private String username;
    @NotNull(message = "{content.empty}", groups = { First.class, Second.class })
    private String content;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }
    }
    public interface First {
    }
    public interface Second {
    }

通过 groups 对验证进行分组

在controler中的代码如下:

    @RequestMapping(value = "/save.action", method = RequestMethod.POST)
    public String save(@Validated( { Second.class }) UserModel userModel, BindingResult result) {
    if (result.hasErrors()) {
    return "validate/error";
    }
    return "redirect:/success";
    }
    @RequestMapping(value = "/update.action", method = RequestMethod.POST)
    public String update(@Validated( { First.class, Second.class }) UserModel user, BindingResult result) {
    if (result.hasErrors()) {
    return "validate/error";
    }
    return "redirect:/success";
    }

2. 组序列

默认情况下,不同组别的约束验证是无序的,然而在某些情况下,约束验证的顺序却很重要,如下面两个例子:(1)第二个组中的约束验证依赖于一个稳定状态来运行,而这个稳定状态是由第一个组来进行验证的。(2)某个组的验证比较耗时,CPU 和内存的使用率相对比较大,最优的选择是将其放在最后进行验证。因此,在进行组验证的时候尚需提供一种有序的验证方式,这就提出了组序列的概念。

一个组可以定义为其他组的序列,使用它进行验证的时候必须符合该序列规定的顺序。在使用组序列验证的时候,如果序列前边的组验证失败,则后面的组将不再给予验证。

下例中声明了组 GroupA.class,GroupB.class 和 Group.class,其中 default,GroupA,GroupB 均为 Group 的序列。

    public interface GroupA {
    }
    public interface GroupB {
    }
    @GroupSequence( { Default.class, GroupA.class, GroupB.class })
    public interface Group {
    }
    public class User {
    @NotEmpty(message = "firstname may be empty")
    private String firstname;
    @NotEmpty(message = "middlename may be empty", groups = Default.class)
    private String middlename;
    @NotEmpty(message = "lastname may be empty", groups = GroupA.class)
    private String lastname;
    @NotEmpty(message = "country may be empty", groups = GroupB.class)
    private String country;
    }
    @RequestMapping(value = "/update.action", method = RequestMethod.POST)
    public String register(@Validated(Group.class) User user, BindingResult result) {
    if (result.hasErrors()) {
    return "validate/error";
    }
    return "redirect:/success";
    }

3. 验证多个对象

当我们在一个功能处理方法上需要验证多个模型对象时,需要通过如下形式来获取验证结果:

    @RequestMapping("/validate/multi")
    public String multi(@Valid @ModelAttribute("a") A a, BindingResult aErrors, @Valid @ModelAttribute("b") B b, BindingResult bErrors) {
    if (aErrors.hasErrors()) { //如果a模型对象验证失败
    return "validate/error";
    }
    if (bErrors.hasErrors()) { //如果a模型对象验证失败
    return "validate/error";
    }
    return "redirect:/success";
    }

每一个模型对象后边都需要跟一个Errors或BindingResult对象来保存验证结果,其方法体内部可以使用这两个验证结果对象来选择出错时跳转的页面或处理的逻辑。

4. Junit测试

当自定义拓展Validation时,可以使用如下方法进行测试:

    @Test
    public void testValidate() {
    AnnotationDescriptor<EqualsAny> descriptor = new AnnotationDescriptor<EqualsAny>(EqualsAny.class);
    EqualsAny equalsAny = AnnotationFactory.create(descriptor);
    EqualsAnyValidator equalsAnyValidator = new EqualsAnyValidator();
    equalsAnyValidator.initialize(equalsAny);
    Assert.assertTrue(equalsAnyValidator.isValid("123", null));
    }

另外再讲一点Spring对自定义JSR-303限制类型支持的新特性,那就是Spring支持往ConstraintValidator里面注入bean对象。例如在EqualsAnyValidator中利用@Resource注解注入其他Bean对象。

 

在使用Validation时,传递参数到国际化资源文件properties

    @NotEmpty(message="{password.empty.error}")
    private String password;

资源文件validation_zh_CN.properties中为

    password.empty.error=password不能为空

实际开发中,很多参数都是要验证非空的,如果每个参数都单独加个错误描述,是很麻烦的。properties虽支持“{}”的写法传递参数,但使用JSR-303注解无法实现传递参数。我想了个办法可通过自定义注解方式实现。

首先,建立个自定义的@NotEmpty注解:

    package com.itkt.payment.core.annotation;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import javax.validation.Constraint;
    import javax.validation.Payload;
    import com.itkt.payment.core.handler.NotEmptyValidator;
    @Retention(RetentionPolicy.RUNTIME)
    @Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER })
    @Constraint(validatedBy = { NotEmptyValidator.class })
    public @interface NotEmpty {
    String field() default "";
    String message() default "{com.itkt.payment.core.handler.NotEmpty.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    }

自定义的NotEmpty注解中,我们新加了field字段,用于标识字段名称。

然后,建立NotNullValidator实现类:

    package com.itkt.payment.core.handler;
    import javax.validation.ConstraintValidator;
    import javax.validation.ConstraintValidatorContext;
    import com.itkt.payment.core.annotation.NotNull;
    public class NotNullValidator implements ConstraintValidator<NotNull, Object> {
    @Override
    public void initialize(NotNull annotation) {
    }
    @Override
    public boolean isValid(Object str, ConstraintValidatorContext constraintValidatorContext) {
    return str != null;
    }
    }

之后,在资源文件validation_zh_CN.properties中,改变写法:

    password.empty.error={field}不能为空

最后,我们就可以在User类中使用自定义的NotEmpty注解:

    @NotEmpty(field = "password", message = "{password.empty.error}")
    private String password;

实际上,国际化资源文件本身支持从JSR-303注解中获取属性的参数值的,例如从@Length注解中,获取min和max属性的值:

    username.length.error=用户名长度必须在{min}-{max}之间

之所以自带的@NotEmpty注解无法实现,是因为没有一个属性能传递字段名,所以通过自定义@NotEmpty注解来拓展个field字段。

 
 
@AssertTrue   //用于boolean字段,该字段只能为true   
@AssertFalse //该字段的值只能为false   
@CreditCardNumber //对信用卡号进行一个大致的验证   
@DecimalMax //只能小于或等于该值   
@DecimalMin //只能大于或等于该值   
@Digits (integer= 2 ,fraction= 20 ) //检查是否是一种数字的整数、分数,小数位数的数字。   
@Email //检查是否是一个有效的email地址   
@Future //检查该字段的日期是否是属于将来的日期   
@Length (min=,max=) //检查所属的字段的长度是否在min和max之间,只能用于字符串   
@Max //该字段的值只能小于或等于该值   
@Min //该字段的值只能大于或等于该值   
@NotNull //不能为null   
@NotBlank //不能为空,检查时会将空格忽略   
@NotEmpty //不能为空,这里的空是指空字符串   
@Null //检查该字段为空   
@Past //检查该字段的日期是在过去   
@Size (min=, max=) //检查该字段的size是否在min和max之间,可以是字符串、数组、集合、Map等   
@URL (protocol=,host,port) //检查是否是一个有效的URL,如果提供了protocol,host等,则该URL还需满足提供的条件   
@Valid //该注解只要用于字段为一个包含其他对象的集合或map或数组的字段,或该字段直接为一个其他对象的引用,   
       //这样在检查当前对象的同时也会检查该字段所引用的对象

以下是分类
Bean Validation 中内置的 constraint
 
@Null     被注释的元素必须为 null
@NotNull     被注释的元素必须不为 null
@AssertTrue     被注释的元素必须为 true
@AssertFalse     被注释的元素必须为 false
@Min(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=)     被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction)     被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past     被注释的元素必须是一个过去的日期
@Future     被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=)     被注释的元素必须符合指定的正则表达式

Hibernate Validator 附加的 constraint
@NotBlank(message =)   验证字符串非null,且长度必须大于0
@Email     被注释的元素必须是电子邮箱地址
@Length(min=,max=)     被注释的字符串的大小必须在指定的范围内
@NotEmpty     被注释的字符串的必须非空
@Range(min=,max=,message=)     被注释的元素必须在合适的范围内

Hibernate validation 注解 springmvc 验证 分组的相关教程结束。

《Hibernate validation 注解 springmvc 验证 分组.doc》

下载本文的Word格式文档,以方便收藏与打印。