public class AddressDTO {
@NotNull
@NotBlank
@Pattern(regexp="(0|91)?[789][0-9]{9}")
String phoneNumber;
}
如何通过使用Hibernate注释为这些DTO类提供优先级验证?
如果要按顺序验证约束并且仅对第一个失败的约束失败,则需要使用组和组序列。
请参阅有关如何使用组序列的文档的这一段:http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-defining-group-sequences。
不过,它会有点冗长。
根据您的要求,另一种可能性是使用约束组合和@ReportAsSingleViolation。见http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-constraint-composition。
最后一个(已经提到过)将使用您自己的验证代码编写自定义约束,以验证您的“约束”,并根据您检查的内容调整错误消息。