users@jersey.java.net

Re: [Jersey] Post binary, xml, json

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 26 Jun 2009 16:17:37 +0200

On Jun 26, 2009, at 4:11 PM, saturon wrote:

>
> But how do I know the type of the converter in the reader?
>

Because the message body reader knows the type it should instantiate,
it will be passed in as a parameter to the readFrom method:

https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/ext/MessageBodyReader.html
#readFrom(java.lang.Class,%20java.lang.reflect.Type,
%20java.lang.annotation.Annotation[],%20javax.ws.rs.core.MediaType,
%20javax.ws.rs.core.MultivaluedMap,%20java.io.InputStream)

That type will be MyConverter.class.

Paul.

> public CDATASupport readFrom(byte[] data, java.lang.reflect.Type
> genericType,
> java.lang.annotation.Annotation[] annotations, MediaType
> mediaType,
> MultivaluedMap<java.lang.String,java.lang.String>
> httpHeaders,
> java.io.InputStream entityStream) {
> //instantiate which type of converter??
> ...
>
> Actually I am decoding the byte[] back to a bean. This bean is wrapped
> by the converter.



>
>
>
> -----Original Message-----
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: Freitag, 26. Juni 2009 14:18
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Post binary, xml, json
>
>
>
> On Jun 26, 2009, at 2:08 PM, saturon wrote:
>
>> Hi
>>
>> I have several POST Resources that must support a custom binary mime
>> type as well as xml and json.
>> So I have:
>> @POST
>> @Consumes({"mobileapp/octet-stream"})
>> public Response post(byte[] data) {
>>
>> Now what I did was just declaring 2 methods for the different types
>> and factor out the common code base. Is there maybe a better way to
>> do this?
>>
>> @POST
>> @Consumes({"mobileapp/octet-stream"})
>> public Response post(byte[] data) {
>> //transform to common type, then process
>> ..
>>
> How are you processing the byte[], are you decoding to an instance of
> MyConverter ?
>
> If so you can write a general MessageBodyReader to operate on the
> converters:
>
> @Consumes("mobileapp/octet-stream")
> public class MyReader implements MessageBodyReader<CDATAProvider> {
> ...
> }
>
> then you can have one resource method:
>
> @POST
> @Consumes({"application/xml", "application/json", "mobileapp/
> octet-stream"})
> public Response post(MyConverter data) { ... }
>
> Paul.
>> @POST
>> @Consumes({"application/xml", "application/json"})
>> public Response post(MyConverter data) {
>> //transform to common type, then process
>> .
>>
>> Rgds Ben
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>