gpt4 book ai didi

java - Bean 验证范围约束

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

我需要对实体数据字段实现范围约束:

@Entity
@Table(name = "T_PAYMENT")
public class Payment extends AbstractEntity {

//....

//Something like that
@Range(minValue = 80, maxValue = 85)
private Long paymentType;

}

我已经创建了验证服务,但必须实现其中的许多情况。

如果插入的数字超出范围,我需要应用程序抛出异常。

最佳答案

你需要 Hibernate Validator ( see documentation )

Hibernate Validator

The Bean Validation reference implementation.

Application layer agnostic validation Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming. But a simple example says more than 1000 words:

public class Car {

@NotNull
private String manufacturer;

@NotNull
@Size(min = 2, max = 14)
private String licensePlate;

@Min(2)
private int seatCount;

// ...
}

关于java - Bean 验证范围约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37782328/

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