简体   繁体   English

Jackson 不序列化字段

[英]Jackson doesn't serialize field

I have the following class -我有以下 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;
    }
}

I am using the objectMapper.valueToTree(sections) to convert it to a JsonNode.我正在使用 objectMapper.valueToTree(sections) 将其转换为 JsonNode。 However when I print the same I see only the id and name field from the Entity class and not anything from the Images list.但是,当我打印相同的内容时,我只看到实体 class 中的 id 和 name 字段,而不是图像列表中的任何内容。 Do I need to add any kind of special logic or annotation to make sure the List gets printed out as well.我是否需要添加任何类型的特殊逻辑或注释以确保列表也被打印出来。

The following is the main class -以下是主要的class——

    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));
    }

You need to add getter and setter for Entity#images您需要为 Entity#images 添加 getter 和 setter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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