首发博客地址
https://blog.zysicyj.top/
一、声明方法约束
1 2 3 4 5 6 7 8 9 10 11 12
| package org.hibernate.validator.referenceguide.chapter03.parameter; public class RentalStation { public RentalStation(@NotNull String name) { } public void rentCar( @NotNull Customer customer, @NotNull @Future Date startDate, @Min(1) int durationInDays) { } }
|
- RentalStation 的 name 不能为空
- rentCar(),customer 不能为空,startDate 不能为空且必须在未来,durationInDays 最少为 1 天
1. 交叉参数约束
1 2 3 4 5 6
| public class Car { @LuggageCountMatchesPassengerCount(piecesOfLuggagePerPassenger = 2) public void load(List<Person> passengers, List<PieceOfLuggage> luggage) { } }
|
该注解通过自动义确保乘客携带不超过 2 件行李
2. 返回值约束
1 2 3 4 5 6 7 8 9 10 11 12 13
| package org.hibernate.validator.referenceguide.chapter03.returnvalue; public class RentalStation { @ValidRentalStation public RentalStation() { } @NotNull @Size(min = 1) public List<@NotNull Customer> getCustomers() { return null; } }
|
- 任何新创建的
RentalStation
对象必须满足 @ValidRentalStation
约束
- 传回的客户清单
getCustomers()
不得为 null
并且必须至少包含元素
- 返回的客户清单
getCustomers()
不得包含任何 null
对象
3. 级联验证
1 2 3 4 5 6 7 8 9 10 11 12 13
| package org.hibernate.validator.referenceguide.chapter03.cascaded; public class Garage { @NotNull private String name; @Valid public Garage(String name) { this.name = name; } public boolean checkCar(@Valid @NotNull Car car) { return false; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package org.hibernate.validator.referenceguide.chapter03.cascaded; public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; public Car(String manufacturer, String licencePlate) { this.manufacturer = manufacturer; this.licensePlate = licencePlate; } }
|
- 验证 checkCar() 方法的参数时,还将 Car 评估对所传递对象的属性的约束。同样,在验证构造函数的返回值时 @NotNull,Garage 将检查对 name 字段的约束
Garage。
- 通常,级联验证对可执行文件的工作方式与对 JavaBeans 属性完全相同。
- 特别是,null 在级联验证期间会忽略值,并且递归执行级联验证
1 2 3 4 5 6 7
| package org.hibernate.validator.referenceguide.chapter03.cascaded.containerelement; public class Garage { public boolean checkCars(@NotNull List<@Valid Car> cars) { return false; } }
|
4. 继承层次结构中的方法约束
- 需要注意的规则
- 方法的调用者必须满足的前提条件不能在子类型中得到加强
- 保证方法调用者的后置条件不会在子类型中减弱
- 子类非法的方法参数约束
1 2 3 4
| package org.hibernate.validator.referenceguide.chapter03.inheritance.parameter; public interface Vehicle { void drive(@Max(75) int speedInMph); }
|
1 2 3 4 5 6 7
| package org.hibernate.validator.referenceguide.chapter03.inheritance.parameter; public class Car implements Vehicle { @Override public void drive(@Max(55) int speedInMph) { } }
|
1 2 3 4
| package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel; public interface Vehicle { void drive(@Max(75) int speedInMph); }
|
1 2 3 4
| package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel; public interface Car { void drive(int speedInMph); }
|
1 2 3 4 5 6 7
| package org.hibernate.validator.referenceguide.chapter03.inheritance.parallel; public class RacingCar implements Car, Vehicle { @Override public void drive(int speedInMph) { } }
|
1 2 3 4 5
| package org.hibernate.validator.referenceguide.chapter03.inheritance.returnvalue; public interface Vehicle { @NotNull List<Person> getPassengers(); }
|
1 2 3 4 5 6 7 8 9
| package org.hibernate.validator.referenceguide.chapter03.inheritance.returnvalue; public class Car implements Vehicle { @Override @Size(min = 1) public List<Person> getPassengers() { return null; } }
|
二、验证方法约束
再来一辆车
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Car2 { public Car2(@NotNull String manufacturer) { } @自定义规则 public Car2() { } public void drive(@Max(75) @Min(22) int speedInMph) { } @Size(min = 1) public List getParams() { return null; } }
|
获取工具
1. 校验普通方法参数
2. 测试普通方法返回值
3. 测试构造方法参数
4. 测试构造方法的返回值
三、内置方法约束
1 2 3 4 5 6 7 8 9 10
| package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert; public class Car { @ParameterScriptAssert(lang = "javascript", script = "luggage.size() <= passengers.size() * 2") public void load(List<Person> passengers, List<PieceOfLuggage> luggage) { } } ``` } }
|