0

I want to have a generic bulk update call, where the client sends an array of objects for me to update. The body of the request would look like this:

Request Body

[
  {
    "id": 1,
    "name": "noe",
    "phone": "1234"
  },
  {
    "id": 2,
    "name": "noea",
    "phone": "1235"
  }
]

I've looked around and it seems like most marshalling providers can handle this in the body of the request. I've tried taking it a few different ways:

E[ ] or List< E > Parameters

How can I open what stream it's trying to read from? Am I missing some annotation or have the wrong @Consumes annotation? I did a little searching and I couldn't find anything that describes the problem I'm having.

    @PUT
    @Consumes( MediaType.APPLICATION_JSON )
    @Produces( MediaType.APPLICATION_JSON )
    @Path( "/multiedit" )
    public Response edit( E[] objects ) {

    }

Or

    @PUT
    @Consumes( MediaType.APPLICATION_JSON )
    @Produces( MediaType.APPLICATION_JSON )
    @Path( "/mutliedit" )
    public Response edit( List<E> entities ) {

    }

Produces

javax.json.JsonException: Unable to detect charset due to Stream closed
root cause: java.io.IOException: Stream closed

Wrapper class

After that I tried packing the array into a wrapper class.

public class EntityList<E> implements Serializable {
    private List<E> entities;
}

@PUT
@Consumes( MediaType.APPLICATION_JSON )
@Produces( MediaType.APPLICATION_JSON )
@Path( "/multiedit" )
public Response edit( EntityList<E> entities ) 
{

}

This causes a mapping exception with my mapper api, Johnzon Mapper.

org.apache.johnzon.mapper.MapperException: Unsupported [<list of objects here>] com.my.project.EntityList<E>

I just want to accept an array of objects. I see it done in a bunch of different questions like here or here, but it doesn't work for me.

UPDATE: I can get the first two options working if I do a non-generic version of it. If I subclass a generic version of it and try to override it, it will still die with the same exceptions as stated.

Community
  • 1
  • 1
GuitarStrum
  • 713
  • 8
  • 24
  • Did you configure a proper reader to do the object parsing? I believe [this](http://stackoverflow.com/questions/31990540/unmarshal-json-to-java-pojo-in-jax-rs) thread can help you. – Vitor Santos May 04 '17 at 17:47

2 Answers2

0

Yeah, I believe you are missing the @QueryParam Annotation.

@PUT
@Consumes( MediaType.APPLICATION_JSON )
@Produces( MediaType.APPLICATION_JSON )
@Path( "/multiedit" )
public Response edit( @QueryParam("entities") EntityList<E> entities ) 
{

}
jiveturkey
  • 2,484
  • 1
  • 23
  • 41
0

you have to use @RequestBody link this :

@PUT
@Consumes( MediaType.APPLICATION_JSON )
@Produces( MediaType.APPLICATION_JSON )
@Path( "/multiedit" )
public Response edit(@RequestBody EntityList<E> entities ) 
{


}
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17