I want to deserialize a non-null field in a request model using custom deserializer in Kotlin like this:
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
data class MyRequest(val foo: Foo) {
data class Foo(val bar: String)
companion object {
object Deserializer : StdDeserializer<Foo>(Foo::class.java) { //This is added to Jackson Module successfully somewhere else
override fun deserialize(jsonParser: JsonParser?, context: DeserializationContext?): Foo {
val node: JsonNode = jsonParser!!.codec.readTree(jsonParser)
return if (node.isNull || node.isTextual.not()) Foo("default")
else Foo(node.asText())
}
}
}
}
But when I send a post request with empty json body, I get this:
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Instantiation of [simple type, class com.me.myapi.model.request.MyRequest] value failed for JSON property foo due to missing (therefore NULL) value for creator parameter foo which is a non-nullable type; nested exception is com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
Since Foo is a non-null type and I did not pass anything in request body for foo, this is being thrown before deserialization. I wonder if there is a way to handle this exception, such as giving a default value and continue with deserialization step.