将休眠验证程序与自定义消息源一起使用时如何转义参数

How to escape arguments when using hibernate validator with a custom messagesource

我有这样的东西:

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

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

一些自定义验证器(具有 values 属性)使用此消息:

validation.constraints.PossibleValues.message=The provided value "${validatedValue}" is invalid. Possible values: {values}

问题是 {}AbstractMessageInterpolator#interpolateMessage 中不再存在,所以我没有得到参数插值。

我试过'{'validatedValue'}'\{等各种转义都没有成功。

有人知道在这种情况下如何转义消息参数吗?

(我看到了this question,但没用)

我使用的配置与您几乎相同:

  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="ISO-8859-1" />
    <property name="fallbackToSystemLocale" value="false" />
    <property name="useCodeAsDefaultMessage" value="false" />
    <property name="basenames" value="classpath:applicationMessages,classpath:ValidationMessages"/>
  </bean>

在我的 ValidationMessages.properties 中,我使用简单的 { 和 } 为插值变量将一些消息覆盖为加利西亚语。例如:

javax.validation.constraints.Min.message=debe ser maior o igual a {value}
...
org.hibernate.validator.constraints.Length.message=debe ter unha lonxitude entre {min} e {max}

唯一奇怪的行为是,如果你想放单引号,你需要用单引号转义它:

org.hibernate.validator.constraints.ScriptAssert.message=o script ''{script}'' non devolve true

希望对您有所帮助。