gpt4 book ai didi

spring-3 - 通过 Spring Framework 编码 Hibernate Validator 自定义错误消息的配置

转载 作者:行者123 更新时间:2023-12-03 06:43:59 26 4
gpt4 key购买 nike

我正在使用 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 已经有

首选项 -> 工作区 -> 使用 UTF-8 的文本文件编码

我需要什么额外的缺失配置?

最佳答案

您不能在属性文件中使用这样的 unicode 字符。请参阅http://docs.oracle.com/javase/8/docs/api/java/util/PropertyResourceBundle.html

In that case, characters that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section 3.3 of The Java™ Language Specification...

á 将是\u00E1 和 ñ\u00F1

关于spring-3 - 通过 Spring Framework 编码 Hibernate Validator 自定义错误消息的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25531112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com