package com.mycompany;
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;
public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
//getters and setters ...
}
@NotNull
,并且是所谓的约束注释,我们用它来声明约束,这些约束应应用于 Car 实例的字段:@Size
@Min
manufacturer
永远不要为空
licensePlate
永远不要为 null,并且长度必须介于 2 到 14 个字符之间
seatCount
应至少为 2。