SpringBootでフォームエンティティのプロパティ(Integer)に対して、
数字以外の文字列を送信した際に、Springのデフォルトのエラーメッセージが
表示されるのですが、それを日本語化(デフォルトを日本語に)したいです。
下記の通りクラスパス配下にメッセージ設定ようのファイルを配置したのですが、
読み込めていないような気がしています。
情報など足りないところあるかもしれませんがわかる範囲でご教授
いただけませんでしょうか?
text
1# src/main/resources/messages_ja.properties 2 3typeMismatch.Integer={0} は数字で入力してください
Java
1// メッセージ設定クラス 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.MessageSource; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7import org.springframework.validation.Validator; 8import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 9import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 10 11@Configuration 12public class MessageConfig extends WebMvcConfigurerAdapter 13{ 14 @Autowired 15 private MessageSource messageSource; 16 17 @Bean 18 public LocalValidatorFactoryBean validator() 19 { 20 LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); 21 localValidatorFactoryBean.setValidationMessageSource(messageSource); 22 return localValidatorFactoryBean; 23 } 24 25 @Override 26 public Validator getValidator() 27 { 28 return validator(); 29 } 30}
Java
1// FORMクラス 2 3import javax.validation.constraints.Pattern; 4import org.hibernate.validator.constraints.NotEmpty; 5import lombok.Data; 6 7@Data 8public class SampleForm { 9 10 private Integer reg_emp_num; 11 12 ... 13}
↓を日本語(設定した内容)にしたい
html
1<!--画面に表示されるデフォルトエラーメッセージ --> 2Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'reg_emp_num'; nested exception is java.lang.NumberFormatException: For input string: "あ"
補足:プロパティファイルにorg.hibernate.validator.constraints.NotEmpty.message={任意のメッセージ}を記入すれば、@NotEmptyをセットしたフィールドに対して設定したメッセージがあたりますが、
typeMismatchについては日本語化がうまくいきません。
SpringBoot 1.4.1
Jdk 1.8.0_121
MySQL 5.6.27
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/18 06:40