使用 Jersey 的 RESTful:POST 多个实体的方法

发布于 2024-09-16 04:57:25 字数 543 浏览 19 评论 0原文

我正在尝试在 Java 的 RESTful Web 服务中开发一种方法,以使用 POST 请求将多个条目插入到 MySQL 数据库中。生成的 RESTful Web 服务具有插入单个实体的方法,但不能插入多个实体。例如,它接受:

<creature>
  <sort>Mouse</sort> 
  <name>Pinky</name>
</creature>

但不接受(我想要的):

<creature>
  <sort>Mouse</sort> 
  <name>Pinky</name>
</creature>
<creature>
  <sort>Elephant</sort> 
  <name>Dumbo</name>
</creature>

我猜你必须循环遍历实体,但不知道如何实现它,作为一个可耻的新手。

I am trying to develop a method in my RESTful web service in Java to insert multiple entries into a MySQL DB using POST request. The generated RESTful Web Service has a method to insert a single entity, but not multiple ones. For example, it accepts:

<creature>
  <sort>Mouse</sort> 
  <name>Pinky</name>
</creature>

But not (what I would like):

<creature>
  <sort>Mouse</sort> 
  <name>Pinky</name>
</creature>
<creature>
  <sort>Elephant</sort> 
  <name>Dumbo</name>
</creature>

I'm guessing that you have to loop through the entities, but not sure how to implement it, being a shameful novice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2024-09-23 04:57:25

我自己刚刚遇到了这个。我需要多个项目的交易帖子,因此在客户端上进行迭代是不可能的。共识似乎是您需要使用与正常资源不同的路径:

http ://chasenlehara.com/blog/creating-restful-web-services/(多资源)

在一个请求中创建多个项目的 RESTful 方式

不过,我找不到太多关于如何使用 Jersey 执行此操作的信息。事实证明,这非常简单。您应该已经拥有用于 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

Just ran into this myself. I need transactional posts of multiple items, so iterating on the client is out of the question. The consensus seems to be that you need to use a separate path from your normal resources:

http://chasenlehara.com/blog/creating-restful-web-services/ (Multi-resources)

RESTful way to create multiple items in one request

I couldn't find much about how to do this with Jersey, though. As it turns out, it's pretty easy. You should already have multi-entity converter and resource classes for GET requests, you just need to specify a path where the server can assume it's going to receive them:

@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);
       }
    }

Then instead of posting

<creature />

to

http://.../resources/creatures

You would post

<creatures>
    <creature />
    <creature />
</creatures>

to

http://.../resources/creatures/multi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文