Reputation: 637
I Have an object :
public class MoQueryData {
private Map<String,Map<String,MoQuery>> data;
public MoQueryData() {
data = new HashMap<>();
}
public MoQueryData(Map<String, Map<String, MoQuery>> data) {
this.data = data;
}
public Map<String, Map<String, MoQuery>> getData() {
return data;
}
public List<MoQuery> getDataForAllVendors() {
return data.values().stream().flatMap(l -> l.values().stream()).collect(Collectors.toList());
}
}
while using it in the controller like this :
@RequestMapping(method = RequestMethod.GET ,value = "/queries/")
public MoQueryData getMoQueriesData() {
return moQuery.getData();
}
we get the following json
{
"data": {....},
"dataForAllVendors": [....]
}
i don't understand why the getDataForAllVendors is being called? with @JsonIgnore it does not happens but i', still trying to understand what happening under the hood.
Upvotes: 0
Views: 2755
Reputation: 7808
Answer provided by @Sebastian Brudzinski is correct answer. However, he didn't explain how to mark method as ignored. To do so mark your method with annotation @JsonIgnore
marks your property or getter method as one to be ignored by Jackson-JSON serialization. So just to summorize the answer - change the method so it doen't look like getter (say rename it to retrieveDataForAllVendors()
) or mark it with annotation @JsonIgnore
will resolve your issue
Upvotes: 0
Reputation: 878
You are getting the dataForAllVendors
field due to the getter-like named method in your class getDataForAllVendors()
. Jackson discovers and serializes all accessible fields by default and the getter named method in your class is therefore discovered too. Renaming this method or marking it as ignored will prevent serialization.
Upvotes: 3