使用Hibernate验证程序替换Spring消息插值参数

使用Hibernate验证程序替换Spring消息插值参数,spring,hibernate,validation,hibernate-validator,Spring,Hibernate,Validation,Hibernate Validator,根据hibernate验证文档,可以在错误消息中相应地替换{min}和{max}参数的情况下执行以下操作 @Size( min = 2, max = 14, message = "The license plate must be between {min} and {max} characters long" ) 我在Spring应用程序中使用这个符号,但这些参数没有被替换。 我返回“车牌长度必须介于最小字符和

根据hibernate验证文档,可以在错误消息中相应地替换{min}和{max}参数的情况下执行以下操作

 @Size(
            min = 2,
            max = 14,
            message = "The license plate must be between {min} and {max} characters long"
    )
我在Spring应用程序中使用这个符号,但这些参数没有被替换。 我返回“车牌长度必须介于最小字符和最大字符之间”

我是否需要以不同的方式配置验证器以使其工作

以下是我的配置:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/> 
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>

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

信息

我找到了原因。这是由属性“useCodeAsDefaultMessage”引起的,我们无法将其设置为“true”,只需使用默认值“false”。如果这是真的,那么只需从“basenames”中的属性文件获取消息,而不要使用类路径根路径或“org/hibernate/validator”中的默认消息文件

您使用哪个版本的hibernate验证程序库?可以显示您的bean配置吗?您正在使用自定义MessageInterpolator吗?使用默认消息插值器时,应替换参数。Spring围绕消息插值有几个自定义点。查看完整配置非常重要。验证的上下文也很重要。我使用的是最新版本(5.1.0)