如何在 Hibernate 验证器中添加自定义错误消息
我有一个像这样的简单类,
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
public class Form implements Serializable {
@NotNull
@Length(min = 2, max = 20)
private String lastName;
}
类路径中有 messages.properties 文件。然后它通过 Spring bean 加载,如下所示,
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="resourceBundleLocator"/>
</property>
</bean>
<bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath*:messages.properties</value>
</list>
</property>
</bean>
我想要的只是获取根据验证的 bean 定制的错误消息。换句话说,我想让每个 ConstraintViolation 对象返回我在属性文件中定义的错误消息,而不是默认消息。是否可以添加具有类似格式 {xxx.yyyy.zzzz} 的值的消息属性并从 messages.properties 文件引用该消息?
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Form>> inputErrors = validator.validate(form);
如果我想自定义@NotNull和@Length的默认消息,我该怎么做?
我在 Hibernate 验证方面的经验是零,任何示例代码或分步指南将不胜感激。
I have a simple class like this,
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
public class Form implements Serializable {
@NotNull
@Length(min = 2, max = 20)
private String lastName;
}
I have messages.properties file in the classpath. It's then loaded via Spring bean as follows,
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="resourceBundleLocator"/>
</property>
</bean>
<bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath*:messages.properties</value>
</list>
</property>
</bean>
All I want is to get error messages customized according to the bean validated. In other words I want to have each ConstraintViolation object return the error message I have defined in my property file instead of the default one. Is it possible to add message property with a value like this format {xxx.yyyy.zzzz} and refer that message from the messages.properties file ?
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Form>> inputErrors = validator.validate(form);
If I want to customize the default message for @NotNull and @Length, how can I do that?
My experience on Hibernate validation is zero, Any sample code or step by step guide would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您拥有包含消息值的资源包文件。
我认为你必须使用 ie
@NotNull
注释的路径手动覆盖消息!
就像
javax.validation.constraints.NotNull=Notnull 错误发生了!
或者类似的东西!
在这里查找:
Hibernate 验证器- 错误消息
希望有帮助
You have this resource bundle file holding the message values.
I think you have to use the path to the i.e.
@NotNull
annotationto override the message by hand!
like
javax.validation.constraints.NotNull=Notnull error happened!
or something like that!
Look here for that :
Hibernate Validator - The error message
Hope that helps