我自己也遇到了这个。我需要多个项目的事务性帖子,因此在客户端上进行迭代是不可能的。共识似乎是您需要使用与正常资源不同的路径:
http://chasenlehara.com/blog/creating-restful-web-services/(多资源)
在一个请求中创建多个项目的 RESTful 方式
不过,我找不到太多关于如何用泽西岛做到这一点的信息。事实证明,这很容易。您应该已经有 GET 请求的多实体转换器和资源类,您只需要指定一个路径,服务器可以假设它将接收它们:
@Path("creatures")
@Stateless
public class CreaturesResource {
...
@POST
@Consumes({"application/xml", "application/json"})
public Response post(CreatureConverter data) {
Creature entity = data.resolveEntity(em);
postCreature(entity);
}
@POST @Path("multi")
@Consumes({"application/xml", "application/json"})
public Response postMulti(CreaturesConverter data) {
Collection<Creature> entities = data.getEntities();
for (Creature c : entities) {
postCreature(c);
}
}
然后而不是发布
<creature />
至
http://.../resources/creatures
你会发布
<creatures>
<creature />
<creature />
</creatures>
至
http://.../resources/creatures/multi