Java SpringDataREST(使用SpringBoot)-Hibernate验证本地化

Java SpringDataREST(使用SpringBoot)-Hibernate验证本地化,java,hibernate,validation,localization,spring-data-rest,Java,Hibernate,Validation,Localization,Spring Data Rest,我目前正在评估。 我从这个简单的例子开始: 它是开箱即用的。但是现在,在下一步中,我想尝试一些验证,看看框架如何反应。所以我只是简单地注释了Person类: @Size(min = 2, message = "{test.error.message}") private String firstName; 验证本身正在工作,我收到一条错误消息。如果我将名为ValidationMessages.properties的文件放在类路径的根目录中,消息就会被解析(请参见原因) 现在,我不想将文件放在

我目前正在评估。
我从这个简单的例子开始:

它是开箱即用的。但是现在,在下一步中,我想尝试一些验证,看看框架如何反应。所以我只是简单地注释了
Person
类:

@Size(min = 2, message = "{test.error.message}")
private String firstName;
验证本身正在工作,我收到一条错误消息。如果我将名为
ValidationMessages.properties
的文件放在类路径的根目录中,消息就会被解析(请参见原因)


现在,我不想将文件放在根目录中,而是想将它们放在子文件夹中(例如,
lang/ValidationMessages.properties
),并使用Spring
MessageSource
,而不是默认方法

经过一些研究,我发现了以下问题:

不幸的是,使用以下bean定义不起作用:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

  <bean id="messageSource"
            class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
      <list>
        <value>lang/ValidationMessages</value>
      </list>
    </property>
  </bean>

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

lang/ValidationMessages
pom.xml中相应的依赖项(以防万一):


org.springframework.boot
弹簧启动启动器数据rest
org.springframework.boot
spring引导启动器数据jpa


有人知道我错过了什么吗?这可能是因为我使用的不是SpringMVC而是SpringDataREST吗?如果是的话,有没有办法让它发挥作用?

经过一些额外的调查(和大量的搜索),我找到了解决问题的方法


问题

Hibernate没有为验证程序工厂使用bean,这就是为什么没有使用
LocalValidatoryBean

有关更多详细信息,请查看
org.hibernate.cfg.beanvalidation.TypeSafeActivator\35; activate(ActivationContext ActivationContext)


第一种方法

您可以使用以下属性指定要使用的工厂:
javax.persistence.validation.factory

不幸的是,这还不能在Spring Boot的
应用程序中使用。属性

(见此)


解决方案

使用链接的GitHub问题中描述的解决方法是可行的。 您必须为Hibernate提供配置:

@Configuration
public class HibernateConfig extends HibernateJpaAutoConfiguration {

  @Autowired
  private ValidatorFactory validator;

  @Override
  protected void customizeVendorProperties(Map<String, Object>     vendorProperties) {
    super.customizeVendorProperties(vendorProperties);
    vendorProperties.put("javax.persistence.validation.factory", validator);
  }
}
@配置
公共类HibernateConfig扩展了HibernateJPA自动配置{
@自动连线
私有验证器;
@凌驾
受保护的空属性(地图供应商属性){
超级定制房地产(卖方房地产);
put(“javax.persistence.validation.factory”,validator);
}
}

使用此方法可以正确解析消息。

HibernateJPA自动配置无法再工作(在sprint引导2.10之后)

如果您使用的是Spring Boot 2.1.0+,则可以这样做:

@Configuration
@Lazy
class SpringValidatorConfiguration {

    @Bean
    @Lazy
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
        return new HibernatePropertiesCustomizer() {

            @Override
            public void customize(Map<String, Object> hibernateProperties) {
                hibernateProperties.put("javax.persistence.validation.factory", validator);
            }
        };
    }
}
@配置
@懒惰的
类SpringValidator配置{
@豆子
@懒惰的
公共HibernatePropertiesCustomizer HibernatePropertiesCustomizer(最终验证程序){
返回新的HibernatePropertiesCustomizer(){
@凌驾
公共void自定义(映射hibernateProperties){
hibernateProperties.put(“javax.persistence.validation.factory”,validator);
}
};
}
}
来自

@Configuration
@Lazy
class SpringValidatorConfiguration {

    @Bean
    @Lazy
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
        return new HibernatePropertiesCustomizer() {

            @Override
            public void customize(Map<String, Object> hibernateProperties) {
                hibernateProperties.put("javax.persistence.validation.factory", validator);
            }
        };
    }
}