users@jersey.java.net

Re: [Jersey] Post binary, xml, json

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

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
>