近期对springboot项目进行国际化配置,按照资料配置自定义的一个messagesource和相应的LocalValidatorFactoryBean
代码如下
@Override
public Validator getValidator() {
return localValidatorFactoryBean();
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(){
LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
validatorFactoryBean.setProviderClass(HibernateValidator.class);
validatorFactoryBean.setValidationMessageSource(kgMessageSource);//注入自定义messagesource
validatorFactoryBean.afterPropertiesSet();
return validatorFactoryBean;
}
结果发现配置文件的国际化配置解析注解参数失败,国际化配置如下
javax.validation.constraints.Min.message=不能超过--{value}
value的值是注解@Min的参数值
查看源码:org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator#resolveMessage
类中有这样一段,三步走,第一步走自定义的messagesource,
private String resolveMessage(String message, Locale locale) {
String resolvedMessage = message;
//走自定义的messagesource
ResourceBundle userResourceBundle = userResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle constraintContributorResourceBundle = contributorResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle defaultResourceBundle = defaultResourceBundleLocator
.getResourceBundle( locale );
String userBundleResolvedMessage;
boolean evaluatedDefaultBundleOnce = false;
do {
// search the user bundle recursive (step 1.1)
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, userResourceBundle, locale, true
);
// search the constraint contributor bundle recursive (only if the user did not define a message)
if ( !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, constraintContributorResourceBundle, locale, true
);
}
// exit condition - we have at least tried to validate against the default bundle and there was no
// further replacements
if ( evaluatedDefaultBundleOnce && !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
break;
}
// search the default bundle non recursive (step 1.2)
resolvedMessage = interpolateBundleMessage(
userBundleResolvedMessage,
defaultResourceBundle,
locale,
false
);
evaluatedDefaultBundleOnce = true;
} while ( true );
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, userResourceBundle, locale, true
);
这段代码参数就将自定义source传递进去且最后一个参数是true,递归查找的意思,然后就将国际化配置中所有的 { } 符号全去掉了导致解析不成功,
求有经验的大佬指导下,怎么解决,版本是hibernate-validator-6.0.16.Final.jar预格式化文本