将Spring资源包与Hibernate Validator一起使用

时间:2014-11-27 15:34:10

标签: java spring validation hibernate-validator tomcat8

第一次在这里发帖。

我正在开发一个JSON Web服务API,我想使用自定义验证消息但没有成功。

这是我的设置(专注于我的问题所涉及的组件。我也使用MyBatis,jUnit ......):

  • Tomcat 8.0.14
  • Spring 4.1.1
  • 泽西岛2.13
  • Jersey-spring3 2.13
  • 泽西豆验证
  • Hibernate Validator 5.0.0.Final

Bean属性验证和类级验证工作正常。 现在我正在尝试设置自定义错误验证消息。

我不想使用JSR-303 /META-INF/ValidationMessages.properties,但我不想使用spring托管包。

在我的applicationContext.xml中,我定义了:

<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="messageSource" />
    </property>
</bean>

<bean name="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="configuration/messages/validation" />
</bean>

我还使用一些属性创建了/WEB-INF/classes/configuration/messages/validation_fr_FR.properties:

model.error=Modèle invalide

现在当我尝试验证我的Bean时,我在日志中得到了这个:

15:47:14,300 DEBUG http-nio-8080-exec-2 resourceloading.PlatformResourceBundleLocator:72 - ValidationMessages not found.
15:47:45,707 DEBUG http-nio-8080-exec-2 resourceloading.PlatformResourceBundleLocator:69 - org.hibernate.validator.ValidationMessages found.

这里没有提到我的捆绑......

在我的JSON回复中,我得到:

{model.error} (path = EventServiceAPIImpl.createOrUpdateEvent.arg0, invalidValue = fr.bz.pdcv.bean.Event@d99af88)

{model.error}未替换为我的自定义消息。

我认为我的ReloadableResourceBundleMessageSource被Hibernate Validator默认配置覆盖。但为什么呢?

我超载谷歌和stackoverflow找到解决方案,我没有找到任何可以帮助我的东西

有人遇到同样的问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

虽然我猜我迟到但可能会帮助其他人。

使用 ValidationMessages.properties 文件重命名 validation_fr_FR.properties ,并将其放在“ src \ main \ resources ”文件夹中。

所以你的最终路径如下:

  

SRC \主\资源\ ValidationMessages.properties

重新启动服务器并尝试呈现验证。

你不再需要 LocalValidatorFactoryBean ResourceBundleMessageSource

Hibernate基本上在默认目录中查找相同的文件名并覆盖默认的 ValidationMessages.properties