gpt4 book ai didi

java - Jackson 没有序列化字段

转载 作者:行者123 更新时间:2023-12-02 09:18:58 29 4
gpt4 key购买 nike

我有以下类(class) -

public class Entity {

private String id;
private String name;

private List<Image> images = new ArrayList<>();

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public void add(Image image) {
this.images.add(image);
}
}

public class Image {

private String width;
private String height;
private String url;

public String getWidth() {
return width;
}

public void setWidth(String width) {
this.width = width;
}

public String getHeight() {
return height;
}

public void setHeight(String height) {
this.height = height;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}

我正在使用 objectMapper.valueToTree(sections) 将其转换为 JsonNode。但是,当我打印相同的内容时,我只看到实体类中的 id 和名称字段,而看不到图像列表中的任何内容。我是否需要添加任何类型的特殊逻辑或注释来确保列表也被打印出来。

下面是主类-

    public static void main(String[] args) {

Entity entity = new Entity();

entity.setId("1");

Image image = new Image();

image.setWidth("300");
image.setHeight("100");
image.setUrl("www.ca.com");

entity.add(image);

ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.valueToTree(entity));
}

最佳答案

您需要为 Entity#images 添加 getter 和 setter

关于java - Jackson 没有序列化字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58827016/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com