2015-10-05 81 views
0

我有这个消息来源 bean。它适用于获取消息,例如从org.springframework.validation.Validator如何使用messageSource bean处理Hibernate验证消息?

@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处理JSR 349验证消息,这样的POJO类:

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 
} 

但错误消息从仍在(本地化)原休眠,这样{org.hibernate.validator.constraints.NotEmpty.message}。但我想用我自己的错误消息。我尝试了很多选择,但没有一个可以工作。

我想保留整个应用程序的一个消息属性文件。

问题

有一些方法如何告诉Spring使用我为messageSource豆?

回答

0

您确定这不起作用吗?(注释行)?将您的消息放置在属性文件(resources/locale/message.properties)中应该可行...

+0

它可以工作,但我想避免手动为所有约束定义消息密钥。 – Artegon

+0

哦,我误解了这个问题。在/ src/main/resources下创建ValidationMessages.properties,并覆盖你想要自己使用的消息,比如“org.hibernate.validator.constraints.NotEmpty.message =你的消息不是empy”。 –