【发布时间】:2020-09-07 17:40:28
【问题描述】:
我正在使用休眠验证器在我的应用程序中实现验证。我还使用自定义注释和自定义验证器来满足业务验证要求。它工作正常,我能够根据实现获得特定的错误消息。但是,现在我还想返回错误代码(类似于下面代码 sn-p 中给出的内容)以及每个验证的错误消息到调用 validate 方法的方法。
我已阅读有关休眠验证的文档,但找不到任何符合此要求的内容。有人可以帮我在这种情况下实现错误代码吗?
@ValidStateInCountry(code=006, message="Invalid state for given country")
@ValidZipInCountry(code=007, message="Invalid zip for given country")
public class Address {
@NotBlank(code=001, message="address line is mandatory")
private String addressLine;
@ValidCountry(code=002, message="Invalid Country name")
@NotBlank(code=003, message="Country name is mandatory")
private String country;
@NotBlank(code=004, message="state is mandatory")
private String state;
@NotBlank(code=005, message="zip is mandatory")
private String zipCode;
}
【问题讨论】:
-
一个好问题。你试过这种方法吗?:stackoverflow.com/a/2911486/3368818
-
@J Woodchuck :是的,我已经尝试过了,但在我的情况下它不起作用..我有多个类级别和属性级别的自定义验证器,每次验证失败我都需要单独的错误代码。
标签: java spring spring-boot validation hibernate-validator