【问题标题】:RESTful using Jersey: method to POST multiple entitiesRESTful 使用 Jersey:发布多个实体的方法
【发布时间】:2011-04-03 00:07:22
【问题描述】:

我正在尝试在我的 RESTful Web 服务中使用 Java 开发一种方法,以使用 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>

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

【问题讨论】:

    标签: rest multiple-entries


    【解决方案1】:

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

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

    RESTful way to create multiple items in one request

    不过,我找不到太多关于如何使用泽西岛做到这一点的信息。事实证明,这很容易。你应该已经有 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
    

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2023-03-09
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      相关资源
      最近更新 更多