如何通过RESTful Web服务在JAX-RS中添加关系? [英] How to add relationships in JAX-RS via RESTful web service?

查看:81
本文介绍了如何通过RESTful Web服务在JAX-RS中添加关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我可以在RESTful Web服务中删除简单的资源(实体)

OK I can GET POST PUT DELETE simple resources (entities) in my RESTful web service

例如.

/rest/foos
/rest/foos/1 

/rest/bars
/rest/bars/1

但是如何处理添加的关系,例如.使用RESTful Web服务在此关系之间使用@ OneToMany,@ ManyToMany. 假设我有几个Foo实体和几个Bar实体如何建立关系Bar 1具有Foo 3等.

But how to handle adding relationships ex. @OneToMany, @ManyToMany between this relationships using RESTful web service. Suppose I have several Foo entities and several Bar entities how to establish relationships Bar 1 has Foo 3 , etc.

我有这种获取这种关系的方法:

I have such approach to GET this relationships:

GET /rest/bars/1/foos 

上面返回与Bar(id = 1)相关的foo的集合

Above returns collection of foos related with Bar(id=1)

我也许会这样:

POST /rest/bars/1/foos   { # Foo json object } 

上面将创建一个新的Foo对象,并在此新对象和Bar(id = 1)之间建立关联.

Above will create new Foo object and make association between this new object and Bar(id=1).

PUT /rest/bars/1/foos/2  { # Foo json object } 

如果与Bar(id = 1)有这样的关联,或者如果Foo表中没有Foo(id = 2)这样的关联,则更新Foo(id = 2).

Above updates Foo(id=2) if there is such association with Bar(id=1) or if there isn't such association and Foo(id=2) exists in Foo table such association will be made.

如果我只想添加/更新没有关联的Foo,我会如下所示:

If I would like to add/update only Foo without association i made sth like below:

POST /rest/foos
PUT  /rest/foos/2 

如果我想删除Foo(id = 2)

If I would like to remove Foo(id=2)

DELETE /rest/foos/2 

如果我只想删除Bar(id = 1)和Foo(id = 2)之间的关联

And if I only want to delete association between Bar(id=1) and Foo(id=2)

DELETE /rest/bars/1/foos/2 

您如何看待这种方法?您将如何正确处理呢?

What do you think about such approach? And how would you handle this correctly?

推荐答案

关于您的OneToMany关系

Regarding your OneToMany relation How to model parent child entities via REST might help you.

与其父级的关联是子级资源的属性,因此OneToMany关系应在许多端进行管理.

The association with its parent is an attribute of the child resource so the OneToMany relation should be managed on the many side.

PUT /rest/bars/1/foos/2  { # Foo json object }

如果您的foos仅可与bar一起使用是可以的,但是如果它们也可以在没有bar的情况下存在,则应使用不隶属于bar资源的专用foo资源.考虑 aggregation composition .

Is OK if your foos are only possible in connection with a bar but if they can also exist without a bar you should use a dedicated foo resource not subordinate to the bar resource. Think aggregation or composition.

如果我只想删除Bar(id = 1)和之间的关联 Foo(id = 2)

And if I only want to delete association between Bar(id=1) and Foo(id=2)

删除/rest/bars/1/foos/2

DELETE /rest/bars/1/foos/2

我不会使用这种方法,而是用id=2更新foo以使其不再与bar 1关联. DELETE /rest/bars/1/foos/2应该删除完整的foo资源,而不仅仅是我认为的关联.

I would not use this approach instead update the foo with id=2 to not be associated with bar 1 anymore. A DELETE /rest/bars/1/foos/2 should delete the full foo resource instead of only the association in my opinion.

关于ManyToMany关系,您将需要第三个资源来直接操作联接表.为了简化一点,该资源仅需要定义POSTDELETE操作. POST为ID添加一个新的关联对:一个为bar,一个为foo. DELETE删除这样的一对.

Regarding ManyToMany relations you'll need a third resource to manipulate the join table directly. To simplify this a little bit this resource will only need the POST and DELETE actions defined. POST to add a new associated pair: to ids, one for bar and one for foo. DELETE to remove such a pair.

这篇关于如何通过RESTful Web服务在JAX-RS中添加关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆