我正在研究Spring Rest和Angularjs。我对如何转换对象有疑问。可以说我有两节课。

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "fooId", scope = Foo.class)
class Foo {
   int fooId;
   String fooName;
   String fooTitle;
   Bar bar;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "barId", scope = Bar.class)
class Bar {
   int barId;
   String barName;
   String barTitle;
   int fooId;
}


当我将带有Bar的Foo对象发送到Angularjs时,我得到的是这样的东西:

{
  "fooId": 1,
  "fooName": "foo-name",
  "fooTitle": "foo-title",
  "bar": 1
}


我期待得到这样的:

{
  "fooId": 11,
  "fooName": "foo-name",
  "fooTitle": "foo-title",
  "bar":  {
      "barId": 22,
      "barName": "bar-name",
      "barTitle": "bar-title",
      "fooId": 11
  }
}


有什么事吗

最佳答案

只需尝试从Bar类中删除@JsonIdentityInfo批注-默认的rest控制器就可以很好地转换没有它的类!

09-18 17:10