简体   繁体   中英

How spring boot serialized objects

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM