Spring数据Rest补丁添加到嵌入列表

Spring数据Rest补丁添加到嵌入列表,spring,mongodb,rest,patch,spring-data-rest,Spring,Mongodb,Rest,Patch,Spring Data Rest,我很难使用SpringDataREST和补丁请求添加到嵌入式列表中。我使用的是MongoDB,所以这里没有JPA连接(ManyToOne等),只是一个普通的、常规的嵌入子类型列表 我的豆子是这样的: class Parent { String name; List<Child> children; } class Child { String name; } curl -d '{"children": [ {"name": "bob"} ] }' -H "Conten

我很难使用SpringDataREST和补丁请求添加到嵌入式列表中。我使用的是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),它似乎无法处理我的场景,但这确实是一个非常简单的用例,有什么想法吗?我错过了什么明显的东西吗


谢谢

问题在于您试图编辑父对象,而不是关系集合

  • 您需要创建一个子对象:
  • 返回新创建的子项的url(在位置标头中或作为响应中的自链接-基于您的SDR设置)

  • 然后将此子项添加到父项的子项集合:
  • 这是一个非常简单的用例。如果您仔细想想,框架如何准确地确定您是要添加到集合中,还是仅仅替换它。只是一个想法。
       POST /api/child  {...}
    
       PATCH /api/parent/123/children
       Content-Type:text/uri-list
       body: ***the URI of the child***