https://blog.zysicyj.top/
package org.hibernate.validator.referenceguide.chapter02.fieldlevel;
public class Car {
@NotNull
private String manufacturer;
@AssertTrue
private boolean isRegistered;
public Car(String manufacturer, boolean isRegistered) {
this.manufacturer = manufacturer;
this.isRegistered = isRegistered;
}
//getters and setters...
}
package org.hibernate.validator.referenceguide.chapter02.propertylevel;
public class Car {
private String manufacturer;
private boolean isRegistered;
public Car(String manufacturer, boolean isRegistered) {
this.manufacturer = manufacturer;
this.isRegistered = isRegistered;
}
@NotNull
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
@AssertTrue
public boolean isRegistered() {
return isRegistered;
}
public void setRegistered(boolean isRegistered) {
this.isRegistered = isRegistered;
}
}
在该类型上加约束时,将会校验每个元素
package org.hibernate.validator.referenceguide.chapter02.containerelement.set;
import java.util.HashSet;
import java.util.Set;
public class Car {
private Set<@ValidPart String> parts = new HashSet<>();
public void addPart(String part) {
parts.add( part );
}
//...
}
Car car = new Car();
car.addPart( "Wheel" );
car.addPart( null );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation =
constraintViolations.iterator().next();
assertEquals(
"'null' is not a valid car part.",
constraintViolation.getMessage()
);
assertEquals( "parts[]." ,
constraintViolation.getPropertyPath().toString() );
也会校验每个元素
package org.hibernate.validator.referenceguide.chapter02.containerelement.list;
public class Car {
private List<@ValidPart String> parts = new ArrayList<>();
public void addPart(String part) {
parts.add( part );
}
//...
}
Car car = new Car();
car.addPart( "Wheel" );
car.addPart( null );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation =
constraintViolations.iterator().next();
assertEquals(
"'null' is not a valid car part.",
constraintViolation.getMessage()
);
assertEquals( "parts[1]."
,
constraintViolation.getPropertyPath().toString() );
package org.hibernate.validator.referenceguide.chapter02.containerelement.map;
import java.util.HashMap;
import java.util.Map;
import javax.validation.constraints.NotNull;
public class Car {
public enum FuelConsumption {
CITY,
HIGHWAY
}
private Map<@NotNull FuelConsumption, @MaxAllowedFuelConsumption Integer> fuelConsumption = new HashMap<>();
public void setFuelConsumption(FuelConsumption consumption, int value) {
fuelConsumption.put( consumption, value );
}
//...
}
Car car = new Car();
car.setFuelConsumption( Car.FuelConsumption.HIGHWAY, 20 );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation =
constraintViolations.iterator().next();
assertEquals(
"20 is outside the max fuel consumption.",
constraintViolation.getMessage()
);
assertEquals(
"fuelConsumption[HIGHWAY].,
constraintViolation.getPropertyPath().toString()
);
Car car = new Car();
car.setFuelConsumption( null, 5 );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation =
constraintViolations.iterator().next();
assertEquals(
"must not be null",
constraintViolation.getMessage()
);
assertEquals(
"fuelConsumption[]. ,
constraintViolation.getPropertyPath().toString()
);
package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;
public class Car {
private Optional<@MinTowingCapacity(1000) Integer> towingCapacity = Optional.empty();
public void setTowingCapacity(Integer alias) {
towingCapacity = Optional.of( alias );
}
//...
}
Car car = new Car();
car.setTowingCapacity( 100 );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation = constraintViolations.iterator().next();
assertEquals(
"Not enough towing capacity.",
constraintViolation.getMessage()
);
assertEquals(
"towingCapacity",
constraintViolation.getPropertyPath().toString()
);
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;
public class Car {
private GearBox<@MinTorque(100) Gear> gearBox;
public void setGearBox(GearBox gearBox) {
this.gearBox = gearBox;
}
//...
}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;
public class GearBox<T extends Gear> {
private final T gear;
public GearBox(T gear) {
this.gear = gear;
}
public Gear getGear() {
return this.gear;
}
}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;
public class Gear {
private final Integer torque;
public Gear(Integer torque) {
this.torque = torque;
}
public Integer getTorque() {
return torque;
}
public static class AcmeGear extends Gear {
public AcmeGear() {
super( 60 );
}
}
}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;
public class GearBoxValueExtractor implements ValueExtractor<GearBox<@ExtractedValue ?>> {
@Override
public void extractValues(GearBox<@ExtractedValue ?> originalValue, ValueExtractor.ValueReceiver receiver) {
receiver.value( null, originalValue.getGear() );
}
}
Car car = new Car();
car.setGearBox( new GearBox<>( new Gear.AcmeGear() ) );
Set> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );
ConstraintViolation constraintViolation =
constraintViolations.iterator().next();
assertEquals(
"Gear is not providing enough torque.",
constraintViolation.getMessage()
);
assertEquals(
"gearBox",
constraintViolation.getPropertyPath().toString()
);
package org.hibernate.validator.referenceguide.chapter02.containerelement.nested;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
public class Car {
private Map<@NotNull Part, List<@NotNull Manufacturer>> partManufacturers =
new HashMap<>();
//...
}
package org.hibernate.validator.referenceguide.chapter02.classlevel;
@ValidPassengerCount
public class Car {
private int seatCount;
private List passengers;
//...
}
在一个类实现接口或扩展另一个类时,在超类上声明的所有约束注释都以与该类本身上指定的约束相同的方式约束
package org.hibernate.validator.referenceguide.chapter02.inheritance;
public class Car {
private String manufacturer;
@NotNull
public String getManufacturer() {
return manufacturer;
}
//...
}
package org.hibernate.validator.referenceguide.chapter02.inheritance;
public class RentalCar extends Car {
private String rentalStation;
@NotNull
public String getRentalStation() {
return rentalStation;
}
//...
}
不仅支持单个对象校验,还支持级联验证
package org.hibernate.validator.referenceguide.chapter02.objectgraph;
public class Car {
@NotNull
@Valid
private Person driver;
//...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph;
public class Person {
@NotNull
private String name;
//...
}
在校验Car的时候,会校验Person,因此若Car引用的Person的name为空,则会校验失败
package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;
public class Car {
private List<@NotNull @Valid Person> passengers = new ArrayList();
private Map<@Valid Part, List<@Valid Manufacturer>> partManufacturers = new HashMap<>();
//...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;
public class Part {
@NotNull
private String name;
//...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;
public class Manufacturer {
@NotNull
private String name;
//...
}
class Car {
@NotNull
@Size(min = 5,max = 20)
private String manufacturer;
@AssertTrue
private boolean isRegistered;
public Car(String manufacturer, boolean isRegistered) {
this.manufacturer = manufacturer;
this.isRegistered = isRegistered;
}
}
09:35:00.446 [main] INFO com.bm.validate.TestValidatorBean - 内插的错误消息:只能为true
09:35:00.446 [main] INFO com.bm.validate.TestValidatorBean - 非插补的错误消息:{javax.validation.constraints.AssertTrue.message}
09:35:00.446 [main] INFO com.bm.validate.TestValidatorBean - 正在验证的根Bean:com.bm.validate.Car@7c83dc97
09:35:00.446 [main] INFO com.bm.validate.TestValidatorBean - 如果是bean约束,则将约束应用到bean实例;如果是属性约束,则是托管该约束的属性的bean实例:com.bm.validate.Car@7c83dc97
09:35:00.447 [main] INFO com.bm.validate.TestValidatorBean - 根bean验证器值的属性路径:isRegistered
09:35:00.447 [main] INFO com.bm.validate.TestValidatorBean - 报告约束失败的原数据:false
09:35:00.447 [main] INFO com.bm.validate.TestValidatorBean - 报告约束失败的元数据:ConstraintDescriptorImpl{annotation=j.v.c.AssertTrue, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@60015ef5, message={javax.validation.constraints.AssertTrue.message}, payload=[Ljava.lang.Class;@2f54a33d}, constraintType=GENERIC, valueUnwrapping=DEFAULT}
检查带注释元素的属性为false
检查带注释元素的属性为True
本文由 mdnice 多平台发布