我在我的项目中使用Java Hibernate的Validation注释。我想验证我收到的数据并返回一组无效字段。这可能吗?
让我们说我有一个名为Car
的
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
@CheckCase(CaseMode.UPPER)
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 ...
}
让我们说我创造了一辆新车:
Car c = new Car("AUDI", NULL, 1);
这不应该是可能的,因为应该填写车牌并且座位数应该大于或等于2.所以我想返回两个元素的数组,说车牌和座位数无效(如果可能的话,附上额外的消息)。
答案 0 :(得分:1)
如ElmerCat所述,您的构造函数应定义为:
public Car (
@NotNull String manufacturer,
@NotNull @Size(min = 2, max = 14) @CheckCase(CaseMode.UPPER) String licensePlate,
@Min(2) int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licensePlate;
this.seatCount = seatCount;
}