使用 Jersey 的 RESTful:POST 多个实体的方法
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己刚刚遇到了这个。我需要多个项目的交易帖子,因此在客户端上进行迭代是不可能的。共识似乎是您需要使用与正常资源不同的路径:
http ://chasenlehara.com/blog/creating-restful-web-services/(多资源)
在一个请求中创建多个项目的 RESTful 方式
不过,我找不到太多关于如何使用 Jersey 执行此操作的信息。事实证明,这非常简单。您应该已经拥有用于 GET 请求的多实体转换器和资源类,您只需要指定一个路径,服务器可以假设它将接收它们:
然后而不是发布
到
您将发布
到
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:
Then instead of posting
to
You would post
to