gpt4 book ai didi

Java Hibernate 验证返回无效字段

转载 作者:行者123 更新时间:2023-12-01 10:43:13 25 4
gpt4 key购买 nike

我正在我的项目中使用 Java Hibernate 的验证注释。我想验证收到的数据并返回无效字段的数组。这可能吗?

假设我有一个名为 Car 的 Java 类:

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。所以我想返回一个由两个元素组成的数组,表示车牌和座位数无效(如果可能的话,带有额外的消息)。

最佳答案

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;
}

关于Java Hibernate 验证返回无效字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com