如何在 Hibernate 验证器中添加自定义错误消息

发布于 2024-11-29 22:43:53 字数 1436 浏览 1 评论 0原文

我有一个像这样的简单类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-12-06 22:43:53

您拥有包含消息值的资源包文件。

我认为你必须使用 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 annotation
to 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文