我正在使用 Spring (4.0.5) 和 Hibernate Validator (5.1.2.Final)
我已经通过 .properties 文件处理了错误消息,它也适用于 i18n。
- 目前,它处于独立和测试(JUnit)环境中。我不想包含 Web 环境以保持简单。
我有以下内容:
对于ValidationMessages_es_PE.properties文件(西班牙语)
person.salary.digits=Dato invalido '${validatedValue}', máximo tamaño no decimal permitido es {integer}, máximo tamaño decimal permitido es {fraction}
关于基础设施
@Configuration
public class ValidatorConfiguration {
@Bean
public ResourceBundleMessageSource resourceBundleMessageSource(){
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("com.manuel.jordan.validation.messages.ValidationMessages");
return resourceBundleMessageSource;
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(){
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(resourceBundleMessageSource());
MessageSourceResourceBundleLocator messageSourceResourceBundleLocator = new MessageSourceResourceBundleLocator(resourceBundleMessageSource());
ResourceBundleMessageInterpolator resourceBundleMessageInterpolator = new ResourceBundleMessageInterpolator(messageSourceResourceBundleLocator);
localValidatorFactoryBean.setMessageInterpolator(resourceBundleMessageInterpolator);
return localValidatorFactoryBean;
}
}
观察我有setDefaultEncoding("UTF-8")
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ValidatorConfiguration.class)
public class InvalidPersonValidation03Test {
private static final Logger logger = LoggerFactory.getLogger(InvalidPersonValidation03Test.class);
@Autowired
private LocalValidatorFactoryBean validator;
public InvalidPersonValidation03Test(){
LocaleContextHolder.setLocale(new Locale("es","PE"));
}
@Test
public void testInvalidNullId(){
...
部分输出如下
- >>>testInvalidFractionSalary
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
字符ñ
并á
没有显示出预期的效果
我的 IDE 已经有了
Preferences -> Workspace -> Text file encoding
使用UTF-8
我需要什么额外的缺失配置?