简体   繁体   English

Spring Boot如何序列化对象

[英]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.

相关问题 如何基于活动配置文件控制是否应使用Spring Boot序列化字段 - How to control if a field should be serialized with Spring Boot based on active profile Spring 引导:如何从 FieldError.getField() 获取序列化名称 - Spring Boot: How to get the Serialized Name from FieldError.getField() 当object需要在spring开机序列化 - When object needs to be serialized in spring boot 如何在 Java Spring 引导应用程序中使用 Web 客户端发送序列化为 REST 调用主体的 Spring Bean? - How to send Spring Bean serialized as the body of a REST call using Web Client in a Java Spring boot application? 如何使用 Spring Boot 返回一组对象? - How to return a set of objects with Spring Boot? 如何更新 Spring Boot 中的嵌套对象列表? - How to update nested list of objects in Spring Boot? 如何按范围spring boot jpa过滤对象? - How to filter objects by scope spring boot jpa? 如何在 Spring boot 中使用 Spring Validation 验证嵌套对象? - How to validate the Nested Objects using Spring Validation in Spring boot? Spring 引导:控制每个请求的序列化响应中是否存在 null 字段 - Spring Boot: Control if null fields present in serialized response per request 如何在Spring Boot中使用JSON对象数组? - How do I consume an array of JSON objects with Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM