kotlin and @Valid Spring annotation

前端 未结 4 1013
陌清茗
陌清茗 2020-12-13 18:21

I have an entity:

class SomeInfo(
        @NotNull @Pattern(regexp = Constraints.EMAIL_REGEX) var value: String) {
    var id: Long? = null
}
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 18:40

    As an alternative to Michal's answer, annotating the getter also works.

    class SomeInfo(
        @get:NotNull
        @get:Pattern(regexp = Constraints.EMAIL_REGEX)
        var value: String
    ) {
        var id: Long? = null
    }
    

    The annoying part is, that not using @get: or @field: will annotate the constructor parameter. This is still valid kotlin code (so you don't get an error). It's just useless in these use cases.

提交回复
热议问题