Spring Data-Rest POST 到子资源 [英] Spring Data-Rest POST to sub-resource
问题描述
假设我有以下结构:
@Entity
class Person extends AbstractPersistable<Long> {
String name
String surname
}
@Entity
class Task extends AbstractPersistable<Long> {
String description
@ManyToOne
Person person
}
如果我遵循适当的 HAL
准则,我就不应该公开实体 ID.由于我没有双向关系,我不能将 PUT
或 PATCH
转换为 http://localhost:8080/persons
.
If I follow proper HAL
guidelines I'm not supposed to expose entity id's. Since I don't have a bi-directional relationship I cant PUT
or PATCH
to http://localhost:8080/persons
.
即使我确实创建了关系,我也可能不想先将 POST
Task
发送到 /tasks
然后PUT
到 /persons
,(移动客户端会杀了我).但即便如此,我也没有返回实体的 Task ID
,因此我可以将 PUT
发送到 Person
实体.(我显然可以进行字符串解析,但我认为这不合适).
Even if I did create the relation, I probably wouldn't want to first POST
the Task
to /tasks
and then PUT
to /persons
, (mobile clients are going to kill me). But even then I don't have the Task ID
even from the returned Entity so I can PUT
to the Person
entity. (I obviously can string parse but I don't think it's appropriate).
我可能也不希望 Person
实体中有 1000 个任务的列表.所以不导出 Task
实体并不是一个真正的选择(这意味着 PATCH
将不起作用)
I probably wouldnt want to have a list of 1000 tasks in the Person
entity either. So not exporting the Task
entity is not really an option (and this means PATCH
will not work)
那么,如果我无法获取他的 ID,我该如何将 Person
与 Task
关联起来?正确的做法是什么?
So how am I supposed to associate the Person
with the Task
if I cannot get his id? What is the correct approach?
推荐答案
如果您想将任务与人员关联,您需要该人员的链接.
If you want to associate a Task with a Person you need the link to the person.
假设人 URI 是 http://localhost/persons/1
然后您可以通过在 person 属性中传递该 URI 来将该人分配给任务.
Then you could assign the person to a task by just passing that URI in the person attribute.
因此,Task 的帖子可能如下所示:
So a post to Task could look like this:
{
"description": "some text",
"person": "http://localhost/persons/1"
}
Spring-data-rest 将查找人员并处理其余部分.
Spring-data-rest will lookup the person and take care of the rest.
这篇关于Spring Data-Rest POST 到子资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!