更新到多关联

use*_*786 5 spring spring-data-rest spring-boot

用户和组之间存在多对多关系。我想知道如何更新与 SDR 的这种关系。这是我在阅读文档后到目前为止所尝试的。

curl -X POST -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users
Run Code Online (Sandbox Code Playgroud)

预期结果:将用户 5 添加到组 1。

实际结果:405 方法不允许。

curl -X PUT -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users
Run Code Online (Sandbox Code Playgroud)

预期结果:用用户 5 替换组 1 的所有成员。

实际结果:按预期工作。

curl -X PUT -H 'Content-Type: text/uri-list' -d @members.txt http://localhost:8080/rest/groups/1/users
Run Code Online (Sandbox Code Playgroud)

其中文件 members.txt 有:

http://localhost:8080/rest/users/5
http://localhost:8080/rest/users/6
http://localhost:8080/rest/users/7
Run Code Online (Sandbox Code Playgroud)

预期结果:将组 1 的所有成员替换为用户 5、6 和 7。

实际结果:仅添加最后一个用户(在本例中为 7)。

有人可以提供一个关于如何向关联添加单个 URI 的示例吗?此外,如果可能,如何添加或替换与多个 URI 的关联?

Bra*_*oks 3

重新阅读文档后,它确实说 POST 应该添加到集合中。

我的经验是使用 PATCH 添加到集合中。

为了进一步回答:您应该能够将 PUT CONTENT-TYPE: text/uri-list 与具有多个 URI 的内容主体一起使用。每个 URI 由换行符“\n”分隔

  • 最后一个测试是使用文件上传。我相信在使用文件时,curl 会去掉新行字符。尝试使用 `curl -X PUT -H 'Content-Type: text/uri-list' -d "http://localhost:8080/rest/users/5 \n http://localhost:8080/rest/users/ 6 \n http://localhost:8080/rest/users/7" http://localhost:8080/rest/groups/1/users` (2认同)
  • 如果您想从集合中删除项目怎么办? (2认同)