2015-10-05 79 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」。 –