【问题标题】:Spring Rest JSON conversionSpring Rest JSON 转换
【发布时间】:2015-12-30 04:20:46
【问题描述】:

我正在研究 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
  }
}

可能出了什么问题?

【问题讨论】:

  • 无法产生您使用spring 4.2.1.RELEASEjackson-databind 2.4.4 获得的结果。我得到了预期的结果。使用 Chrome Rest Client 进行测试

标签: java angularjs json spring rest


【解决方案1】:

只需尝试从您的 Bar 类中删除 @JsonIdentityInfo 注释 - 默认的休息控制器在没有它的情况下可以很好地转换类!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2022-12-12
    相关资源
    最近更新 更多