I am trying to deserialize nested JSON into a single DTO in a Spring Boot application. The JSON:
{
"values":[
{
"name":"test",
"map":{
"d1":"d1Value",
"d2":"d2Value",
"d3":"d3Value"
}
},
{
"name":"test1",
"map":{
"d1":"d1Value",
"d4":"d4Value"
}
}
]
}
My kotlin classes:
data class A(
val values: List<B>,
)
data class B(
val name: C,
val map: Map<D, String>,
)
data class C(@JsonValue val value: String)
data class D(@JsonValue val value: String)
When I try deserialize json using jacksonObjectMappermapper.readValue<A>(jsonObject)
I receive error:
Cannot construct instance of `...C` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('test')
CodePudding user response:
Assuming C
and D
are always String
, simply use String
in the model as follows:
data class A(
val values: List<B>,
)
data class B(
val name: String,
val map: Map<String, String>,
)
CodePudding user response:
I figured out what was the problem, the solution:
data class C(@get:JsonValue val value: String)
data class D(@get:JsonValue val value: String)