Jersey, how to POST a list of JSON objects?

前端 未结 5 1474
栀梦
栀梦 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 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.

提交回复
热议问题