Spring,REST,Jackson和循环层次结构

时间:2016-11-13 22:08:09

标签: java spring spring-mvc jackson

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

class Parent {
  Long id;
  List<Child> children;
}

class Child {
  Long id;
  Parent parent;
}

我希望能够提出两种类型的REST请求:

    应该返回的
  1. / parents / 1:

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

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

    目前我已经找到了两个选择:

    1. 使用自定义@JsonView(WithParent),@ JsonView(WithChild)
    2. 使用DTO方法

0 个答案:

没有答案