杰克逊未对字段进行序列化 [英] Jackson doesn't serialize field

查看:59
本文介绍了杰克逊未对字段进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程-

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.但是,当我打印相同的内容时,我仅从Entity类中看到id和name字段,而从Images列表中看不到任何内容.我是否需要添加任何种类的特殊逻辑或注释以确保同时打印列表.

I am using the objectMapper.valueToTree(sections) to convert it to a 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. Do I need to add any kind of special logic or annotation to make sure the List gets printed out as well.

以下是主要类-

    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

You need to add getter and setter for Entity#images

这篇关于杰克逊未对字段进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆