Bean验证自定义消息永远不会起作用

时间:2017-10-27 12:48:53

标签: spring hibernate bean-validation

将此验证(hibernate-validator 6.0.4.Final)放入我的Spring Boot 1.4.6应用程序中:

public class Author {
@Past @NotNull
private LocalDate birth;
@PastOrPresent
private LocalDate death;
...
@AssertTrue(message = "{javax.validation.constraints.author.death}") // custom message code
private boolean isValid() {
    return death == null || death.isAfter(birth);
}
}

我的自定义消息永远不会上升。即使MessageResource是正确的:

@Autowired
/** see "spring.messages.basename" application property */
private MessageSource messageSource;

@Override // WebMvcConfigurerAdapter
public Validator getValidator() {
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    factory.setValidationMessageSource(messageSource); // custom code ok at DEBUG
    return factory;
}

我知道有很多关于这个问题的问题和页面。但是几个小时后我仍然无法上班。即使采用@Bean方法:

@Bean
public LocalValidatorFactoryBean validator(MessageSource messageSource) {
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    factory.setValidationMessageSource(messageSource); // DEBUG still ok
    return factory;
}

有什么好建议,有人吗?我一定错过了什么......

  

javax.validation.ConstraintViolationException:对于组[javax.validation.groups.Default,]的持续时间,类[com.mycompany.demo.domain.Author]的验证失败   违反约束的清单:[       ConstraintViolationImpl {interpolatedMessage =' {javax.validation.constraints.author.death}',propertyPath = valid,rootBeanClass = class com.mycompany.demo.domain.Author,messageTemplate =' {javax。 validation.constraints.author.death}'}   ]

注意:没有"消息"在注释中指定的参数,默认消息被正确转换和本地化(interpolatedMessage)

0 个答案:

没有答案