我搜索问题并尝试应用解决方案,但结果是属性键未转换为消息。
这是验证配置:
@Configuration
@ComponentScan("simgenealogy")
public class ValidatorInternational {
@Bean
public ReloadableResourceBundleMessageSource validationMessageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:static/ValidationMessages");
messageSource.setCacheSeconds(10); // reload messages every 10 seconds
return messageSource;
}
@Bean
public Validator validator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource((MessageSource) validationMessageSource());
return validator;
}
}
ValidationMessages.properties和ValidationMessages_pl.properties位于ressources下的/ static目录中。
@NonEmptyString(message = "{first.name.empty}")
private String firstName;
这就是我所说的约束违规:
constraintException.getConstraintViolations().forEach(
c -> {
log.error(LogString.MSG_CONSTRAINT_VIOLATION, c.getMessage());
showNotification(String.format(
MessagesString.MSG_CONSTRAINT_VIOLATION,
c.getMessage()
)
);
}
);
并生成日志:
2019-07-31 15:24:51.867 ERROR 38764 --- [ion Thread] s.a.g.s.w.ServiceGuard :
Constraint violation: {first.name.empty}
2019-07-31 15:24:51.911 ERROR 38764 --- [ion Thread] s.a.g.s.w.ServiceGuard :
Constraint violation: {last.name.empty}
为了验证,我使用:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.1.0.Alpha5</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>