In my Spring application that is built with Kotlin I would like to use bean validation on a data class that looks like this.
data class CustomerDto(
@field: NotBlank
val firstName: String,
@field: NotBlank
val lastName: String)
On sending a post with an empty firstName to the customer endpoint I would like to get the constraint validations but due to the fields not allowing null values I don't get the validations but rather get the following error.
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Instantiation of [simple type, class pkg.CustomerDto] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type; nested exception is com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class pkg.CustomerDto] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type\n at [Source: (PushbackInputStream); line: 19, column: 1] (through reference chain: pkg.CustomerDto[\"firstName\"])",
"path": "/shop/5/customer"
Is there any other option to mark the dto field as not optional and still get the constraint violations? When I mark them as being optional I have to use the !! on the not nullable fields in the code when mapping them to my entities.
Thanks.