[英]Spring Boot with Kotlin - @Value annotation not working as expected
[英]Spring boot validation annotation not working for Kotlin
我為 Kotlin 獲得了一個簡單的數據類
data class Person(val name: String, @get: Min(18) val age: Int)
我實際上從一個 CSV 文件構建了這個類,我使用 apache CSV 解析器讀取了 CSV。 即使我有一些age
字段小於 18 的數據,測試仍然沒有通過錯誤。
看起來這個注釋不適用於 Kotlin?
您當前正在使用Min
注釋來注釋構造函數參數 - 我相信您應該使用如下所示的使用站點目標來注釋該字段:
data class Person(val name: String, @field:Min(18) val age: Int)
嘗試將注釋@Validated
添加到類中(盡管可能只適用於 spring bean)。 這些注釋不會阻止設置值,它們只是允許從現有實例獲取帶有驗證錯誤列表的BindingResult
。 如果您需要阻止設置值,您可以使用自定義委托。
val age: Int by MyDelegates.min(18)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.