Jersey, how to POST a list of JSON objects?

前端 未结 5 1358
栀梦
栀梦 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条回答
  • 2021-02-05 22:54

    Ok, so in the end I solved this using a simple wrapper class in order to generate { items : [{ <myEnityInstanceJson1> }, { <myEnityInstanceJson2> }, ... ]}. I guess there is a way to have a generic wrapper but for now this will do:

    @XmlRootElement
    public class MyEntityWrapper implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private List<MyEntity> items;
    
        public MyEntityWrapper() {
            this.items = new ArrayList<MyEntity>();
        }
    
        public MyEntityWrapper(List<MyEntity> items) {
            this.items = items;
        }
    
        public List<MyEntity> getItems() {
            return items;
        }
    
        public void setItems(List<MyEntity> items) {
            this.items = items;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 22:59

    This is valid JSON for an array:

    {"elements": [
            {"field1" : value1, "field2" : value2}, 
            {"field1" : value3, "field2" : value4},
            ...]
    };
    

    (see here for an example)

    You don't need to send text, you can send it as JSON. Also your MyEntity should have @XmlRootElement on it (see here, section 5.2 for an example).

    You don't need PathParam in your arguments, someParam is available when the request is posted if you leave the @Path("/some-path/{someParam}") in your method signature .

    0 讨论(0)
  • 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<MyEntity> 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}, ...]
    }
    
    0 讨论(0)
  • 2021-02-05 23:05

    Wrapper class works. MyEntity[] myEnts doesn't work.

    This is what I did and it worked.

    public String createBatch(@PathParam("someParam") String someParam,
                                                      String content)
    

    Use ObjectMapper to convert to List of objects.

    List<MyEntity> entities = om.readValue(content, 
                                           new TypeReference<List<MyEntity>>(){}).
    
    0 讨论(0)
  • 2021-02-05 23:11

    The problem is the generic list type, which is not available at runtime due to type erasure, so Jersey wont know what kind of POJOs to unmarshal.

    I think the simplest solution (which I know works, at least when using Jackson in your MessageBodyReader) in this case would be to just use a normal Java array instead of the List, so the method signature becomes:

    public String createBatch(@PathParam("someParam") String someParam, MyEntity[] myEnts)
    

    And yes, combining @PathParam and a consumed/unmarshalled body parameter should be fine.

    0 讨论(0)
提交回复
热议问题