JAX-RS Post multiple objects

Thizzer picture Thizzer · Apr 5, 2011 · Viewed 79.4k times · Source

I have a method;

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
public void test(ObjectOne objectOne, ObjectTwo objectTwo)

Now I know I can post a single object in json format, just putting it into the body. But is it possible to do multiple objects? If so, how?

Answer

tine2k picture tine2k · Jan 8, 2013

You can not use your method like this as correctly stated by Tarlog.

However, you can do this:

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
public void test(List<ObjectOne> objects)

or this:

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
public void test(BeanWithObjectOneAndObjectTwo containerObject)

Furthermore, you can always combine your method with GET parameters:

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
public void test(List<ObjectOne> objects, @QueryParam("objectTwoId") long objectTwoId)