質問編集履歴
3
タイトル修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Kotlin の Bean Validation でフィールド名を message に埋め込みたい
|
1
|
+
Spring Boot(Kotlin) の Bean Validation でフィールド名を message に埋め込みたい
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Kotlin の Bean Validation でフィールド名をエラーメッセージに埋め込むことができません。
|
1
|
+
Spring Boot(Kotlin) の Bean Validation でフィールド名をエラーメッセージに埋め込むことができません。
|
2
2
|
|
3
3
|
|
4
4
|
|
2
説明を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,10 @@
|
|
5
5
|
`nameは必須項目です。` のようにエラーメッセージに対象のフィールド名を含めたいのですが、
|
6
6
|
|
7
7
|
`{0}は必須項目です。` とそのまま返却されてしまいます。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
リクエストはJSONで受け取ります。
|
8
12
|
|
9
13
|
|
10
14
|
|
1
設定ファイルのコードを記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,17 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
`nameは必須項目です。` のようにエラーメッセージに対象のフィールド名を含めたいのですが、
|
6
|
+
|
7
|
+
`{0}は必須項目です。` とそのまま返却されてしまいます。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
Java の同様のプロジェクトを参考にしたのですが、Kotlin では他に必要な考慮があるのでしょうか?
|
12
|
+
|
13
|
+
|
14
|
+
|
5
|
-
NameForm.kt
|
15
|
+
* NameForm.kt
|
6
16
|
|
7
17
|
|
8
18
|
|
@@ -24,7 +34,7 @@
|
|
24
34
|
|
25
35
|
|
26
36
|
|
27
|
-
messages.propeties
|
37
|
+
* messages.properties
|
28
38
|
|
29
39
|
|
30
40
|
|
@@ -36,10 +46,56 @@
|
|
36
46
|
|
37
47
|
|
38
48
|
|
39
|
-
上記の設定で、`nameは必須項目です。`というエラーメッセージが返却されてほしいのですが、
|
40
|
-
|
41
|
-
|
49
|
+
* AppConfig.kt
|
42
50
|
|
43
51
|
|
44
52
|
|
53
|
+
```Kotlin
|
54
|
+
|
55
|
+
import org.springframework.context.MessageSource
|
56
|
+
|
57
|
+
import org.springframework.context.annotation.Bean
|
58
|
+
|
59
|
+
import org.springframework.context.annotation.Configuration
|
60
|
+
|
61
|
+
import org.springframework.context.support.ReloadableResourceBundleMessageSource
|
62
|
+
|
63
|
+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
@Configuration
|
68
|
+
|
69
|
+
class AppConfig {
|
70
|
+
|
71
|
+
@Bean
|
72
|
+
|
73
|
+
fun messageSource(): MessageSource {
|
74
|
+
|
75
|
+
val messageSource = ReloadableResourceBundleMessageSource()
|
76
|
+
|
77
|
+
messageSource.setBasename("classpath:messages")
|
78
|
+
|
45
|
-
|
79
|
+
messageSource.setDefaultEncoding("UTF-8")
|
80
|
+
|
81
|
+
return messageSource
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
@Bean
|
88
|
+
|
89
|
+
fun getValidator(): LocalValidatorFactoryBean {
|
90
|
+
|
91
|
+
val bean = LocalValidatorFactoryBean()
|
92
|
+
|
93
|
+
bean.setValidationMessageSource(messageSource())
|
94
|
+
|
95
|
+
return bean
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
```
|