Hi Everyone! I have one easy question How i can ...
# spring
h
Hi Everyone! I have one easy question How i can write @NotNull, @NotEmpty and @NotBlank in kotlin
e
@get:NotNull
etc. Although I'm not sure why you would need
@NotNull
🙂
another option is using
init { require(someStringProperty.isNotEmptyOrBlank()) }
, but that doesn't follow Spring conventions I suppose
h
I'm converting a Java code to kotlin
💡 2
b
@Emil Kantis Jackson will set non-nullable fields to null using reflection.
💣 1
t
personally I’d rather have the json representation accepts nulls and being annotated with
@NotNull
than having the field not-nullable, it makes for easier and better error message given the stack I use (spring boot, web and validation).
@NotNull
generates a validation exception which I can treat as all other validations exceptions, which not-nullable field is a kotlin binding error, which needs special treatment. eg
Copy code
class SomeApiRepresentation(@get:NotNull aNotNullableField: String?)
the price of course is
!!
, but I am not a purist, and in this case
!!
is fine with me
t
I also would vote for @thanksforallthefish’s approach, as a field with null value is a possible input and only validation may fail. You can have a look at bootify.io, it generates a REST controller for a Kotlin/Spring project