JSR-303校验指定顺序

JSR-303介绍

如果要指定校验顺序,可通过groups来控制。

  • 新建几个Group接口
public interface Group1 {
}
public interface Group2 {
}
public interface Group3 {
}
  • 新增一个GroupSequence
@GroupSequence({
        // 默认的必须加上,不然没有指定groups的注解不会生效
        Default.class,
        Group1.class,
        Group2.class,
        Group3.class,
})
public interface ValidationGroupSequence {
}

使用方式:

validator.validate(obj, ValidationGroupSequence.class);

测试用例:

import junit.framework.TestCase;
import org.hibernate.validator.constraints.Length;

import javax.validation.ConstraintViolation;
import javax.validation.GroupSequence;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.Set;

/**
 * @author tanghc
 */
public class ValidatorTest extends TestCase {

    private static javax.validation.Validator validator;

    static {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    /**
     * 测试JSR-303注解校验顺序,校验顺序: Group1~GroupN
     */
    public void testValidate() {
        Set<ConstraintViolation<User>> set = validator.validate(new User("1", 1), ValidationGroupSequence.class);
        if (set != null && set.size() > 0) {
            ConstraintViolation<User> oneError = set.iterator().next();
            String errorMsg = oneError.getMessage();
            throw new IllegalArgumentException(errorMsg);
        }
    }


    private static class User {

        // 如果字段为空,无论如何都会命中这个
        @NotBlank(message = "NotBlank", groups = Group1.class)
        // 优先校验Group2
        // 可交换下面Group2,Group3,看下校验顺序
        @Length(min = 2, max = 20, message = "length must 10~20", groups = Group2.class)
        @Pattern(regexp = "[a-zA-Z]*", message = "name must letters", groups = Group3.class)
        private String name;

        @Min(value = 1, message = "min 1")
        private int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }


    

    public interface Group1 {
    }

    public interface Group2 {
    }

    public interface Group3 {
    }

    @GroupSequence({
            // 默认的必须加上,不然没有指定groups的注解不会生效
            javax.validation.groups.Default.class,
            Group1.class,
            Group2.class,
            Group3.class,
    })
    public interface ValidationGroupSequence {
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值