Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
杰克逊没有';在Spring中序列化嵌套类_Spring_Rest_Jackson - Fatal编程技术网

杰克逊没有';在Spring中序列化嵌套类

杰克逊没有';在Spring中序列化嵌套类,spring,rest,jackson,Spring,Rest,Jackson,我有一个REST前端UI的实现和一个基于SpringJPA的后端。 在它里面,我有一个这样的类: public class TaskInfo { // 4 fields private Parent parentList; // 3 fields // Getters and Setters } class Parent { // Parent class code } 当我尝试获取响应时,我发现在父项的位置,我得到了一个空值。为什么此父对

我有一个REST前端UI的实现和一个基于SpringJPA的后端。 在它里面,我有一个这样的类:

public class TaskInfo {

    // 4 fields

    private Parent parentList;

    // 3 fields

    // Getters and Setters

}

class Parent {

    // Parent class code
}
当我尝试获取
响应
时,我发现在
父项
的位置,我得到了一个
空值
。为什么此
父对象未序列化?有解决办法吗?或者我应该直接在这个类中包含
Parent
的字段吗


编辑:我正在使用Jackson进行序列化。

对我来说,下面的工作很好。我认为这相当于你问题中的代码

package jackson;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TaskInfo {

    public TaskInfo(String id, Parent parentList) {
        super();
        this.id = id;
        this.parentList = parentList;
    }

    private String id;
    private Parent parentList;

    public String getId() {
        return id;
    }

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

    public Parent getParentList() {
        return parentList;
    }

    public void setParentList(Parent parentList) {
        this.parentList = parentList;
    }

    public static void main(String args[]) throws IOException {
        Parent parent = new Parent("123");
        TaskInfo taskInfo = new TaskInfo("taskID", parent);
        String json = new ObjectMapper().writeValueAsString(taskInfo);

        System.out.println(json);
    }

}

class Parent {

    public Parent(String parentId) {
        this.parentId = parentId;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    private String parentId;
    // Parent class code
}
它打印以下输出

{“id”:“taskID”,“parentList”:{“parentId”:“123”}

我用过杰克逊

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.1</version>
</dependency>

com.fasterxml.jackson.core
杰克逊核心
2.7.1

它不会序列化,因为您的属性不是公共的。将属性公开,它就会工作

你在用gson吗?还是杰克逊?。你能用它更新这个问题吗?可能是因为你的类有一个包可见性@SamuelAlexander,相关问题@萨缪拉莱山德·杰克逊。我刚刚做了一个示例,它与Jacksona完美配合。你确定,parentList有一个值集吗?我不知道为什么会为null。我已经追踪到了这个问题,所以这一定是问题所在。也许我忘了在什么地方有一个setter。属性在Java中没有设置为public。方法是。