[英]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 我们得到以下json
{
"data": {....},
"dataForAllVendors": [....]
}
i don't understand why the getDataForAllVendors is being called? 我不明白为什么要调用getDataForAllVendors? with @JsonIgnore it does not happens but i', still trying to understand what happening under the hood.
使用@JsonIgnore不会发生,但是我仍然在尝试了解幕后情况。
You are getting the dataForAllVendors
field due to the getter-like named method in your class getDataForAllVendors()
. 由于类
getDataForAllVendors()
类似getter的命名方法,因此您正在获取dataForAllVendors
字段。 Jackson discovers and serializes all accessible fields by default and the getter named method in your class is therefore discovered too. Jackson默认情况下会发现并序列化所有可访问字段,因此您的类中的getter命名方法也将被发现。 Renaming this method or marking it as ignored will prevent serialization.
重命名此方法或将其标记为已忽略将防止序列化。
Answer provided by @Sebastian Brudzinski is correct answer. @Sebastian Brudzinski提供的答案是正确的答案。 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. 为此,使用注解
@JsonIgnore
标记您的方法,将您的属性或getter方法标记为Jackson-JSON序列化将忽略的方法。 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 因此,仅汇总答案即可-更改方法,使其看起来不像getter(例如,将其重命名为
retrieveDataForAllVendors()
)或使用批注@JsonIgnore
标记将解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.