【发布时间】:2016-01-01 21:57:38
【问题描述】:
我有这个消息来源 bean。它适用于获取消息,例如来自org.springframework.validation.Validator。
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setCacheSeconds(-1);
messageSource.setFallbackToSystemLocale(false);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setBasename("classpath:/locale/messages");
return messageSource;
}
我想使用这个 bean 来处理此类 POJO 类的 JSR 349 验证消息:
public class AuthorizationRequest {
@NotEmpty
//@NotEmpty(message = "validation.notEmpty")
@JsonProperty("response_type")
private String responseType;
@NotEmpty
//@NotEmpty(message = "validation.notEmpty")
@JsonProperty("client_id")
private String clientId;
@NotEmpty
//@NotEmpty(message = "validation.notEmpty")
@JsonProperty("redirect_uri")
private String redirectUri;
private String scope;
// the rest omitted
}
但是来自的错误消息仍然是(本地化的)原始 Hibernate,例如 {org.hibernate.validator.constraints.NotEmpty.message}。但我想使用我自己的错误信息。我尝试了很多选项,但没有一个有效。
我想为整个应用程序保留一个消息属性文件。
问题
有什么方法可以告诉 Spring 使用我的 messageSource bean?
【问题讨论】:
标签: spring hibernate validation