Jersey, how to POST a list of JSON objects?

前端 未结 5 1430
栀梦
栀梦 2021-02-05 22:25

I am building a RESTful web-service in Java using Jersey 1.11, and have problems implementing a method which consumes a list of JSON-ised entities. The single instance method wo

5条回答
  •  旧时难觅i
    2021-02-05 23:02

    I think PathParam and also a Param which should unmarshalled by Jersey(JAX-RS) is not possible. Please try to remove the PathParam Parameter.

    And if you need the second Parameter so create a new class like this

    @XmlRootElement(name = "example")
    public class Example {
      @XmlElement(name = "param")
      private String param;
      @XmlElement(name = "entities")
      private List entities;
    }
    

    and also modify your Methode :

    @POST
    @Path("/some-path")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String createBatch(Example example)
    {
       ... 
    }
    

    your JSON Should look like this:

    {
     "param":"someParam",
     "entities":[
       {"field1" : value1, "field2" : value2}, {"field1" : value3, "field2" : value4}, ...]
    }
    

提交回复
热议问题