Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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
Java Spring、REST、Jackson和循环层次结构_Java_Spring_Spring Mvc_Jackson - Fatal编程技术网

Java Spring、REST、Jackson和循环层次结构

Java Spring、REST、Jackson和循环层次结构,java,spring,spring-mvc,jackson,Java,Spring,Spring Mvc,Jackson,假设我有以下实体层次结构 class Parent { Long id; List<Child> children; } 我希望能够提出两种类型的REST请求: /应返回的家长/1: { "id": "1", "children": [{"id": 1, /* probably some other fields */}] } /儿童/1 { "id": "1", "parent": {"id": 1, /* some other fields */} }

假设我有以下实体层次结构

class Parent {
  Long id;
  List<Child> children;
}
我希望能够提出两种类型的REST请求:

  • /应返回的家长/1:

    {
     "id": "1",
     "children": [{"id": 1, /* probably some other fields */}]
    }
    
  • /儿童/1

    {
     "id": "1",
     "parent": {"id": 1, /* some other fields */}
    }
    
  • 使用Spring和Jackson返回此类域对象视图的最佳方式或最佳实践是什么

    目前我已经想出了两个选择:

  • 要使用自定义@JsonView(带父项),@JsonView(带子项)
  • 使用DTO方法
  • {
     "id": "1",
     "parent": {"id": 1, /* some other fields */}
    }