Spring Boot(Kotlin) の Bean Validation でフィールド名をエラーメッセージに埋め込むことができません。
nameは必須項目です。
のようにエラーメッセージに対象のフィールド名を含めたいのですが、
{0}は必須項目です。
とそのまま返却されてしまいます。
リクエストはJSONで受け取ります。
Java の同様のプロジェクトを参考にしたのですが、Kotlin では他に必要な考慮があるのでしょうか?
- NameForm.kt
Kotlin
1import javax.validation.constraints.NotBlank 2 3data class NameForm( 4 @field:NotBlank 5 var name: String? 6)
- messages.properties
javax.validation.constraints.NotBlank.message={0}は必須項目です。
- AppConfig.kt
Kotlin
1import org.springframework.context.MessageSource 2import org.springframework.context.annotation.Bean 3import org.springframework.context.annotation.Configuration 4import org.springframework.context.support.ReloadableResourceBundleMessageSource 5import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean 6 7@Configuration 8class AppConfig { 9 @Bean 10 fun messageSource(): MessageSource { 11 val messageSource = ReloadableResourceBundleMessageSource() 12 messageSource.setBasename("classpath:messages") 13 messageSource.setDefaultEncoding("UTF-8") 14 return messageSource 15 } 16 17 @Bean 18 fun getValidator(): LocalValidatorFactoryBean { 19 val bean = LocalValidatorFactoryBean() 20 bean.setValidationMessageSource(messageSource()) 21 return bean 22 } 23}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。