I have defined a Kotlin data class like this:
@JsonIgnoreProperties(ignoreUnknown = true)
data class MandatoryLimits(
val upper: Double,
val lower: Double
)
as part of a compound object that my Spring service receives as a request body in a POST request. When I send a test request with one value missing, e.g. like this:
"specificationLimits": {
"lower": 1.6
}
then I receive a correctly deserialised object of Type MandatoryLimits, but the missing value is set to simply 0.0.
I would have expected to get a BAD REQUEST from the service, instead. Do I need to configure something in addition?
Thanks in advance for all your help!
To fail deserialization when a Kotlin primitive is null, you need to make sure the DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES
Jackson configuration flag is active.
Below is an example of ObjectMapper
bean configuration that will throw an error when it tries deserializing a null
Kotlin primitive.
@Configuration
class JacksonConfig {
@Bean
fun objectMapper(): ObjectMapper {
return jacksonObjectMapper()
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
}
}