How can @NotNull be handled for spring validation....
# spring
s
How can @NotNull be handled for spring validation. For example in the following request I want spring to check that the fields are not null and send an error if they are.
Copy code
data class NameRequest(@field:NotNull val firstName: String, @field:NotNull val lastName: String)
But when making the request, I get a 500 server error. java.lang.IllegalArgumentException: Parameter specified as non-null is null: method NameRequest.<init>, parameter firstName", Do all fields have to be marked nullable? I used the spring starter for kotlin which contains the kotlin-jackson dependency.
f
I don't know about the Spring validation process, but with Hibernate validator I need to add the annotations like this :
@get:TheAnnotation theField
d
If you mark your property as type
String
in Kotlin, the Kotlin compiler will add a check in the constructor that the passed in value is not null and throw an exception instead (the one you are seeing). Yes, if you want it to be possible for the field to hold
null
values, then you need to mark it
String?
.
s
Ah okay, so it looks like if i want to respond with a 400 http error and customer message than I will have to make them nullable so that a 500 error isn't thrown