为什么 Hibernate Validator @NotEmpty 会产生重复消息?
在使用 Hibernate Validator 时,我注意到 @NotEmpty
和 @NotNull
注释在 getInvalidValues 返回的
。InvalidValue
数组中生成重复消息(...)
如果我指定一条类似 @NotEmpty(message = "My error message.")
的消息,那么我将得到一个“My error message”的 InvalidValue。 和第二个“可能不为空或空”
如果我不包含消息(例如 @NotEmpty
本身),那么我会得到 InvalidValue 的两个副本,其消息字段为“可能”不为 null 或为空”。
为什么 Hibernate Validator 这样做? 难道我不应该收到一条消息,要么是我使用参数覆盖的值,要么是默认消息,但不是两者都收到?
更多上下文:
我正在使用我自己的 ClassValidator
实现扩展 ClassValidator
。 我这样做是为了添加一些自定义验证这是无法通过注释完成的。 我需要查看该类的多个属性的运行时值才能确定验证。
当我调用 myClassValidator.getInvalidValues() 时,我会得到验证,我会覆盖它。 在 getInvalidValues()
的实现中,我调用 super.getInvalidValues()
来创建初始错误列表,然后将自定义错误添加到该列表中。 在任何情况下,对 super.getInvalidValues() 的调用都包含重复的消息,其中一个与传递到注释中的消息属性匹配,第二个与注释消息的库存值匹配。
While working with Hibernate Validator, I noticed that the @NotEmpty
and @NotNull
annotations produce duplicate messages in the InvalidValue
array returned by getInvalidValues(...)
.
If I specify a message like @NotEmpty(message = "My error message.")
, then I'll get one InvalidValue of "My error message." and a second of "may not be null or empty"
If i don't include a message (eg @NotEmpty
by itself), then I get two copies of the InvalidValue with a message field of "may not be null or empty".
Why does Hibernate Validator do this?? Shouldn't I get one message, either the value that I override using the parameter, or the default message, but not both??
For some more context:
I am extending ClassValidator<T>
with my own ClassValidator<MyClass>
implementation. I do so to add some custom validations which cannot be done by annotation. I need to see the run time value of more than one property of the class in order to determine the validation.
I get the validations when I call myClassValidator.getInvalidValues(), which I override. Inside my implementation of getInvalidValues()
I call super.getInvalidValues()
to create the initial error list, and then I add my custom errors to that list. In any case, the call to super.getInvalidValues()
contains the duplicate messages, one matching the message property passed into the annotation, and a second with the stock value of the annotation message.
Justin,
过去几个月我一直在使用 Hibernate Validator。 虽然我没有遇到您所描述的相同问题,但我也没有扩展
ClassValidator
。 对于自定义验证, Hibernate 参考指南 表明编写自定义约束是正确的方法。我几乎只能在当前的项目中使用内置约束。 在一种情况下,我需要对整数字段进行一些非常具体的计算,我编写了一个自定义约束,如参考指南中所述; 轻而易举。
谈到您的具体问题,我编写了一个简单的测试应用程序作为我的一种健全性检查:
在
validateMe
字段上有两个 Hibernate 约束,控制台输出为:删除一个或另一个具有仅将一条消息打印到控制台的预期效果。
我希望这是有帮助的。
布莱恩·T·格兰特
Justin,
I've been working with Hibernate Validator for the last couple of months. While I have not run into the same issue that you've described, I also have not extended
ClassValidator
. For custom validation, the Hibernate Reference Guide indicates that writing custom constraints is the way to go.I have been able to use the built-in constraints almost exclusively on my current project. In one case, where I needed to do some very specific calculations on an integer field, I wrote a custom constraint as described in the reference guide; it was a breeze.
Speaking to your specific problem, I wrote a simple test app as a sort of sanity check on my part:
With both Hibernate constraints on the
validateMe
field, the console output is:Removing one or the other has the expected effect of printing only a single message to the console.
I hope this is helpful.
Brian T. Grant