How to add elements in a many-to-many relationship via Spring's @RepositoryRestResource REST API?

瘦欲@ 提交于 2019-11-30 22:26:07
Shawn Sherwood

I found an answer here: How to update reference object in Spring-data rest?

By using "Content-Type: text/uri-list" instead of JSON, it is possible to "add" a resource to the collection with a PUT and pass in the URI. You can remove the resource with a DELETE.

After some digging, I discovered that the Spring documentation does describe this: http://docs.spring.io/spring-data/rest/docs/2.2.0.RELEASE/reference/html/#repository-resources.association-resource.

I always hated that text/uri-list content-type, so I did some research and it turned out there is also an undocumented JSON format which can be used:

{
  "_links":{
    "rel":"/555",
    "rel":"/556"
  }
}

The rel of the links could be anything except empty string, they could be all the same. The link part could be the whole URL form the self link of the referenced object, but the last part of the URL is enough. ( forseslash +id)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!