users@jersey.java.net

RE: [Jersey] Post binary, xml, json

From: saturon <netbeans_at_vtxfree.ch>
Date: Fri, 26 Jun 2009 16:11:14 +0200

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

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