[Jersey] Multipart responses

From: Jason Lee <>
Date: Tue, 28 Feb 2012 16:22:41 -0800

I'm trying to figure out how best to handle multipart *responses*. For
the most part (no pun intended :), I have it working. Here's my server

     public Response get() throws MessagingException, IOException {
         MimeMultipart multiPart = new MimeMultipart();
         multiPart.addBodyPart(createBodyPart("pic1.jpg", new
MediaType("image", "jpg").getType()));
         multiPart.addBodyPart(createBodyPart("pic2.png", new
MediaType("image", "png").getType()));

         return Response.ok(multiPart,

and here's my client/test code:

         Response cr =
         Multipart entity = cr.readEntity(Multipart.class);
         Object o = cr.getEntity();
         String string = cr.readEntity(String.class);

There's a lot of cruft there, as I'm trying to figure out how best to
handle the client side (as well as learn the JAX-RS 2.0 Client API).
The cr.readEntity(Multipart.class) returns null, as does cr.getEntity(),
but cr.readEntity(String.class) returns something like this:

Content-Type: image

Content-Type: image


I'm getting the response I'm expecting, but I'm not sure how best to
extract that into something usable on the client. I was hoping that
Jersey would automagically parse all of that for my if I were to request
Multipart.class, but it doesn't seem to want to. Am I doing something
wrong, or am I going to have to parse this response manually?

Jason Lee
Senior Member of Technical Staff
GlassFish Team
Oracle Corporation
Phone +1 405-216-3193