hibernate_validator_06
认证组(校验组)
校验组能够让你在验证的时候选择应用哪些约束条件. 这样在某些情况下( 例如向导 ) 就可以
对每一步进行校验的时候, 选取对应这步的那些约束条件进行验证了. 校验组是通过可变参数传
递给 validate , validateProperty 和 validateValue 的.
注意:
如果某个约束条件属于多个组,那么各个组在校验时候的顺序是不可预知的. 如果
一个约束条件没有被指明属于哪个组,那么它就会被归类到默认组
( javax.validation.groups.Default ).
第一步我们创建两个接口作为认证组:
package test01; /**
* @author Administrator
*汽车驾驶员检查组
*/
public interface DriverChecks { } package test01; /**
* @author Administrator
*汽车车辆检查
*/
public interface CarChecks { }
第二步我们定义Person类,具有name的属性
package test01; import javax.validation.constraints.NotNull; public class Person {
/**
* 姓名
*/
@NotNull
private String name;
public Person(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} }
第三步:创建一个驾驶人继承Person类:
package test01; import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Min; public class Driver extends Person {
/**
* 年龄
*/
@Min(value = 18, message = "You have to be 18 to drive a car", groups = DriverChecks.class)
public int age; /**
* 是否有驾照
*/
@AssertTrue(message = "You first have to pass the driving test", groups = DriverChecks.class)
public boolean hasDrivingLicense; public Driver(String name) {
super( name );
} public void passedDrivingTest(boolean b) {
hasDrivingLicense = b;
} /**
* @return the age
*/
public int getAge() {
return age;
} /**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
} /**
* @return the hasDrivingLicense
*/
public boolean isHasDrivingLicense() {
return hasDrivingLicense;
} /**
* @param hasDrivingLicense the hasDrivingLicense to set
*/
public void setHasDrivingLicense(boolean hasDrivingLicense) {
this.hasDrivingLicense = hasDrivingLicense;
} }
第四步:创建一个车Car类
package test01; import javax.validation.Valid;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; public class Car { /**
* 制造商
*/
@NotNull
private String manufacturer; /**
* 牌照
*/
@NotNull
@Size(min = 2, max = 14)
private String licensePlate; /**
* 座位数
*/
@Min(2)
private int seatCount; /**
* 是否经过车辆检查
*/
@AssertTrue(message = "The car has to pass the vehicle inspectionfirst", groups = CarChecks.class)
private boolean passedVehicleInspection; /**
* 驾驶员
*/
@Valid
private Driver driver; public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
/**
* @return the manufacturer
*/
public String getManufacturer() {
return manufacturer;
}
/**
* @param manufacturer the manufacturer to set
*/
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
/**
* @return the licensePlate
*/
public String getLicensePlate() {
return licensePlate;
}
/**
* @param licensePlate the licensePlate to set
*/
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
/**
* @return the seatCount
*/
public int getSeatCount() {
return seatCount;
}
/**
* @param seatCount the seatCount to set
*/
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
/**
* @return the passedVehicleInspection
*/
public boolean isPassedVehicleInspection() {
return passedVehicleInspection;
}
/**
* @param passedVehicleInspection the passedVehicleInspection to set
*/
public void setPassedVehicleInspection(boolean passedVehicleInspection) {
this.passedVehicleInspection = passedVehicleInspection;
}
/**
* @return the driver
*/
public Driver getDriver() {
return driver;
}
/**
* @param driver the driver to set
*/
public void setDriver(Driver driver) {
this.driver = driver;
} }
现在, 在我们的例子中有三个不同的校验组, Person.name, Car.manufacturer,
Car.licensePlate 和 Car.seatCount都属于默认( Default ) 组, Driver.age 和
Driver.hasDrivingLicense 从属于 DriverChecks 组, 而Car.passedVehicleInspection
在 CarChecks 组中. 下面演示了如何让 Validator.validate 验证不同的组来得到
不同的校验结果.
最后,进行验证:
package test01; import static org.junit.Assert.*; import java.util.Set; import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.groups.Default; import org.junit.BeforeClass;
import org.junit.Test; public class GroupTest {
private static Validator validator; /**
* 获取一个验证器
*/
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
} @Test
public void driveAway() {
// create a car and check that everything is ok with it.
Car car = new Car("Morris", "DD-AB-123", 2);
Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
assertEquals(0, constraintViolations.size());
// but has it passed the vehicle inspection?
constraintViolations = validator.validate(car, CarChecks.class);
assertEquals(1, constraintViolations.size());
assertEquals("The car has to pass the vehicle inspectionfirst",constraintViolations.iterator().next().getMessage());
// let's go to the vehicle inspection
car.setPassedVehicleInspection(true);
assertEquals(0, validator.validate(car).size());
// now let's add a driver. He is 18, but has not passed the driving test
// yet
Driver john = new Driver("John Doe");
john.setAge(18);
car.setDriver(john);
constraintViolations = validator.validate(car, DriverChecks.class);
assertEquals(1, constraintViolations.size());
assertEquals("You first have to pass the drivingtest",constraintViolations.iterator().next().getMessage());
// ok, John passes the test
john.passedDrivingTest(true);
assertEquals(0, validator.validate(car, DriverChecks.class).size());
// just checking that everything is in order now
assertEquals(0, validator.validate(car, Default.class, CarChecks.class, DriverChecks.class).size());
}
}
其实我们发现:
即使
passedVehicleInspection的默认值是 false 也不会校验出错误来. 因为定义在这个属性上的约束
条件并不属于默认的校验组, 接下来,我们来校验 CarChecks 这个组, 这样就会发现car违反了约束
条件, 必须让这个车先通过检测. 接下来,我们给这个车增加一个司机, 然后在基于 DriverChecks 来
校验, 会发现因为这个司机因为还没有通过驾照考试, 所以又一次得到了校验错误, 如果我们设
置passedDrivingTest属性为 true 之后, DriverChecks 组的校验就通过了.
hibernate_validator_06的更多相关文章
随机推荐
- ZOJ-2112-Dynamic Rankings(线段树套splay树)
题意: 完成两个操作: 1.询问一个区间里第k小的数: 2.修改数列中一个数的值. 分析: 线段树套平衡树,线段树中的每个节点都有一棵平衡树,维护线段树所记录的这个区间的元素.这样处理空间上是O(nl ...
- 【转】google chrome如何设置主页
原文网址:http://jingyan.baidu.com/article/8275fc86bf916c46a13cf666.html google chrome是一款拥有众多优秀插件的浏览器,是我们 ...
- Android数据加密解密
最近项目在维护过程中,估计这一周都会没有什么事情做了.于是开始打量自己做完的这个项目,项目在展示方面乏善可陈,然后仔细的想了想,这个项目的亮点无非就在数据加密和解密这一块了.因为是银行的项目,所以对数 ...
- 数据结构(Splay平衡树):HAOI2008 排名系统
[HAOI2008] 排名系统 [题目描述] 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录 ...
- Nodejs in Visual Studio Code 09.企业网与CNPM
1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...
- 常考的算法及Java知识总结
算法 1 字符串模式匹配问题 2 排列组合问题 3 查找排序问题 数据结构 B树(B,B*,B+,红黑树)和二叉树的区别,MAP,hashmap, JAVA: 线程sleep,wait,wake(), ...
- JavaScript中bind、call、apply函数用法详解
在给我们项目组的其他程序介绍 js 的时候,我准备了很多的内容,但看起来效果不大,果然光讲还是不行的,必须动手.前几天有人问我关于代码里 call() 函数的用法,我让他去看书,这里推荐用js 写服务 ...
- Java父类子类的对象初始化过程
摘要 Java基本的对象初始化过程,子类的初始化,以及涉及到父类和子类的转化时可能引起混乱的情况. 1. 基本初始化过程: 对于一个简单类的初始化过程是: static 修饰的模块(static变量和 ...
- Sublime编辑器 前端 必备插件
sublime编辑器前端必备插件 下面这一行是Package Control包安装,它是sublime的插件包管理器.新安装的sublime 里没有Package Control,按一下ctrl+~, ...
- mysql group by的用法 注意
group by 用法: 官方的解释:select 后面的字段必须出现在 group by 后面, 除非是聚合,sum,或者count 但是如果 是多表联查, SELECT c.`name` A ...