Multipart MessageBodyReader MessageBodyWriter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Multipart MessageBodyReader MessageBodyWriter

mathewch
I have an application with Java/Spring/Jersey/Jboss. In one of the services, I want to use Multipart in the response. I want to embed a byte[] into the Multipart. How can I achieve this? I couldn't find a useful example of MessageBodyReader and MessageBodyWriter. Any help is highly appreciated. Thanks Mathew
Reply | Threaded
Open this post in threaded view
|

Re: Multipart MessageBodyReader MessageBodyWriter

Paul Sandoz
Administrator
Hi Mathew,

The jersey-multipart module provides readers and writers for use by  
the client and server:

   https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-multipart/index.html

   https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html 
#d4e1636

Paul.

On Jun 29, 2010, at 1:54 PM, mathewch wrote:

>
> I have an application with Java/Spring/Jersey/Jboss. In one of the  
> services,
> I want to use Multipart in the response. I want to embed a byte[]  
> into the
> Multipart. How can I achieve this? I couldn't find a useful example of
> MessageBodyReader and MessageBodyWriter. Any help is highly  
> appreciated.
> Thanks Mathew
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Multipart-MessageBodyReader-MessageBodyWriter-tp5234675p5234675.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Multipart MessageBodyReader MessageBodyWriter

mathewch
Hi Paul,

Appreciate your quick response.

I managed to have it working this way:

In my services:

MultiPart multiPart = new MultiPart();
multiPart.bodyPart(new BodyPart(response.getReceipts(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
return Response.status(Status.OK).entity(multiPart).type(MediaType.MULTIPART_FORM_DATA).build();


In my client:
MultiPart multiPart = clientResponse.getEntity(MultiPart.class);
byte[] response = multiPart.getBodyParts().get(0).getEntityAs(byte[].class);

Is this the right way to do it?

Thanks,
Mathew
Reply | Threaded
Open this post in threaded view
|

Re: Multipart MessageBodyReader MessageBodyWriter

Paul Sandoz
Administrator
Hi Mathew,

Your code looks almost fine. When returning a response you can do:

   Response.ok(multiPart, MultiPartMediaTypes.MULTIPART_MIXED).build();

Also note the media type is not really correct as you are not  
returning form data (with named body parts) so multipart/mixed is more  
appropriate.

Paul.

On Jun 29, 2010, at 5:18 PM, mathewch wrote:

>
> Hi Paul,
>
> Appreciate your quick response.
>
> I managed to have it working this way:
>
> In my services:
>
> MultiPart multiPart = new MultiPart();
> multiPart.bodyPart(new BodyPart(response.getReceipts(),
> MediaType.APPLICATION_OCTET_STREAM_TYPE));
> return
> Response
> .status
> (Status
> .OK).entity(multiPart).type(MediaType.MULTIPART_FORM_DATA).build();
>
>
> In my client:
> MultiPart multiPart = clientResponse.getEntity(MultiPart.class);
> byte[] response =  
> multiPart.getBodyParts().get(0).getEntityAs(byte[].class);
>
> Is this the right way to do it?
>
> Thanks,
> Mathew
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Multipart-MessageBodyReader-MessageBodyWriter-tp5234675p5235417.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Multipart MessageBodyReader MessageBodyWriter

mathewch
Hi Paul,

I modified the response to use MultiPartMediaTypes.MULTIPART_MIXED.

Thanks,
Mathew