Using Jackson 2.9.6
I have the following data class definition:
data class SomeDTO @JsonCreator internal constructor
(
@get:JsonProperty(value = "first-property", required = true)
@param:JsonProperty(value = "first-property", required = true)
val firstProperty: Long?,
@get:JsonProperty(value = "second-property")
@param:JsonProperty(value = "second-property")
val secondProperty: Int = 1234
@get:JsonProperty("third-property", required = true)
@param:JsonProperty("third-property", required = true)
val thirdProperty: Int
)
What I expect for a JSON that deserializes into SomeDTO
- If
firstPropertyis missing, it should throw an exception. - If
firstPropertyis null, it should assign null since it is nullable. - If
secondPropertyis missing or null it should assign the default value of1234 - If
thirdPropertyis missing or null, it should throw an exception.
Basically that I can control which values can be deserialized and into what.
What I am experiencing:
If not using KotlinModule then (1), (2), and (4) work but (3) fails with:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot map
nullinto type int (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)
If using KotlinModule then (2), (3), and (4) work but (1) fails.
Main part of ObjectMapper configuration:
disable(
MapperFeature.AUTO_DETECT_CREATORS,
MapperFeature.AUTO_DETECT_FIELDS,
MapperFeature.AUTO_DETECT_GETTERS,
MapperFeature.AUTO_DETECT_IS_GETTERS
)
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
registerModule(ParanamerModule())
registerModule(KotlinModule()) // Might be registered or not
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
I think it is easier to use the KotlinModule but I don't know how to tell it to not auto assign null values to nullable fields when the property is missing in the JSON
@fieldin place of@getor@param?@JsonCreatorannotation.