Spring Data Rest PATCH添加到嵌入列表中

问题描述 投票:0回答:1

我在使用Spring Data Rest和PATCH请求添加到嵌入列表时遇到困难。我使用的是MongoDB,所以这里没有JPA联接(ManyToOne等),只是一个普通的老式常规嵌入式子类型列表。

我的豆子看起来像这样:

class Parent {
  String name;
  List<Child> children;
}

class Child {
  String name;
}

我的请求看起来像这样:

curl -d '{"children": [ {"name": "bob"} ] }' -H "Content-Type: application/json" -X PATCH http://localhost:8080/api/parent/123

结果是,所有子元素都被请求中的新子元素替换,例如

old:     [ 'tom', 'sally' ]
request: [ 'bob' ]
expected result: [ 'tom', 'sally', 'bob']
actual result:   [ 'bob' ]

我已经完成了Spring代码(DomainObjectReader),似乎并不能解决我的问题,但是可以肯定,这是一个非常简单的用例,有什么想法吗?我是否缺少明显的东西?

谢谢!

spring mongodb rest patch spring-data-rest
1个回答
0
投票

问题是您尝试编辑父对象,而不是关系集合。

  1. 您需要创建一个孩子:
   POST /api/child  {...}

这将返回到新创建的子项的URL(根据位置的SDR设置,在位置标头中或作为响应中的自身链接)

  1. 然后将此孩子添加到父母的孩子集合中:
   PATCH /api/parent/123/children
   Content-Type:text/uri-list
   body: ***the URI of the child***
© www.soinside.com 2019 - 2024. All rights reserved.