On the server side, I have a method which looks like this:
@POST
@Path("/foods/{year}/{month}/{day}")
@Consumes("multipart/mixed")
@Produces("application/json; charset=UTF-8")
@Transactional
public boolean setFoodsForTheDay(@PathParam("year") int year, @PathParam("month") int month,
@PathParam("day") int day, @Multipart(value = "foodList", type = MediaType.APPLICATION_JSON) Food[] foodList) {
if (log.isDebugEnabled()) {
log.debug("list size={}", foodList.size());
}
doStuff(foodList);
}
If I send in the following POST request to /foods/2013/06/26 it will actually work and the array will get parsed correctly:
Host: localhost:7777
Accept: application/json
Content-Type: multipart/mixed; boundary=---------------------------25819220131967
Content-Length: 226
-----------------------------25819220131967\r\n
Content-Disposition: form-data; name="foodList"\r\n
Content-Type: application/json\r\n
\r\n
[ {"id":null,"name":"Banana","recipe":null} ]\r\n
-----------------------------25819220131967--\r\n
As you can see, it's important to send in a multipart/mixed (or perhaps multipart/form-data would work too) because then I can set the Content-Type of the part, and it will get parsed correctly.
This all works. Now the problem is, I need to send this request in with jQuery (or any other Ajax tool) and looks like it's not possible to send multipart/mixed? Or there's some trick with an iframe but it will still not be possible to set the Content-type of the part.
Does anyone know of a solution for this problem? How can I send in an array of objects to the server in JSON serialization?