【问题标题】:Spring Data Rest: how to save a property that is an array of another entity?Spring Data Rest:如何保存作为另一个实体数组的属性?
【发布时间】:2016-05-26 16:22:23
【问题描述】:

我正在尝试发送 POST 请求以将实体保存在 Spring Data Rest 存储库中。

保存没问题,但只适用于字符串等简单属性...

但是当我必须保存一个复杂的属性(如 List otherEntities)时,相关的对象并没有链接到资源中。

我正在尝试以这种格式发送 JSON:

{
    "property": "value",
    "otherEntities" : "http://localhost:8080/myapp/api/otherEntities/1"
}

它保存的对象,但是链接:

http://localhost:8080/myapp/api/objects/1/otherEntities

返回“otherEntities”的空数组,而不是在 url 处可访问的对象:

http://localhost:8080/myapp/api/otherEntities/1

任何想法都将不胜感激。

【问题讨论】:

    标签: spring-mvc spring-data-rest spring-hateoas


    【解决方案1】:

    首先,如果你想 POST 到一个 List 属性,你必须用 [] 包围它。所以 JSON 格式必须是:

    {
        "property": "value",
        "otherEntities" : ["http://localhost:8080/myapp/api/otherEntities/1"]
    }
    

    其次,我想您使用的是@OneToMany 或@ManyToMany 关系。这些注释在其中一个关系侧具有属性“mappedBy”。请注意,您只能从不包含属性“mappedBy”的一侧保存关系。如果从另一端保存关系,则不会保存。

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2020-04-22
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2021-06-16
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多