Im trying to Deserialize a hashmap using jackson. My data structure is
public class FilterDto {
private Map<String, List<String>> sectorFilter;
public FilterDto() {}
public Map<String, List<String>> getSectorFilter() {
return sectorFilter;
}
public void setSectorFilter(Map<String, List<String>> sectorFilter) {
this.sectorFilter = sectorFilter;
}
}
My request body is
{
"sectorFilter": [
{
"ab": [
"abc",
"cde"
]
}
]
}
When i tried to deserialize this i got
Cannot deserialize instance of `java.util.Map<java.lang.String,java.util.List<java.lang.String>>` out of START_ARRAY token
So i tried using a custom deserialzer to see whether it would work. I created a custom deerialzer like below
public class FilterDeserializer extends StdDeserializer<Map<String,List<String>>> {
public FilterDeserializer(StdDeserializer<?> src) {
super(src);
}
@Override
public Map<String,List<String>> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
}
}
and added it to my dto
@JsonDeserialize(using = FilterDeserializer.class)
private Map<String, List<String>> sectorFilter;
but now im getting
No qualifying bean of type 'com.fasterxml.jackson.databind.deser.std.StdDeserializer<?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
any idea how can i get this work ? Thanks
CodePudding user response:
sectorFilter
, as you’ve shown, isn’t a map, it’s an array of JSON objects. Try to deserialize as List<Map<String, List<String>>>
. Of course, you can also create classes mimicking that structure if you want.