users@jersey.java.net

Re: [Jersey] Using MessageBodyReader

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 22 Aug 2008 09:43:30 +0200

Hi Kem,

Arul is correct in that MessageBodyReaders/Writers only operate on
the body of a request or response, notice that they operate on bytes
and not characters.

There are two ways you can do this one portable and one Jersey specific.

The portable way:

   public BufferedImage draw(@QueryParam("s") ShapeParam s) { ... }

   ...

   public class ShapeParam {
      public ShapeParam(String param) {
         // parse param to create a Shape instance
      }

      public getShape() { ... }

   }


The Jersey way:


   public BufferedImage draw(@QueryParam("s") Shape s) { ... }

   ...

   @Provider
   public MyShapeProvider implements InjectableProvider<QueryParam,
Parameter> {
     public final Scope getScope() {
         return Scope.PerRequest;
     }

         public Injectable<Shape> getInjectable(ComponentContext ic,
                 QueryParam a, Parameter c) {
             if (!Shape.class.isAssignableFrom(c.getParameterClass()))
                 return null;

             final String name = c.getSourceName();
             return new Injectable<Shape>() {
                 public Map<String, String> getValue(HttpContext c) {
                     String value = c.getUriInfo().getQueryParameters
().getFirst(name);
                     // Instantiate Shape from name:
                     Shape s = ...
                     return s;
                 }
             };
         }
   }

Although i really should try and make this a bit easier to manage for
common cases e.g. the handling of default values.

Paul.

On Aug 22, 2008, at 12:33 AM, Kem Elbrader wrote:

> The developer guide state that "the MessageBodyReader is used to map
> an HTTP request entity body to method parameters"[1] Here is an
> example http://weblogs.java.net/blog/mhadley/archive/2008/02/
> integrating_jer_2.html
>
> I want to have a get method that takes a javax.awt.Shape which is
> retrieved from a query parameter.
> For example,
> http://localhost:8080/?s=r:10,20 might return an image of a rectangle
> with dimension 10 wide and 20 high and
> http://localhost:8080/?s=o:10,20 would return an oval with those
> dimensions.
> http://localhost:8080/?s=p:(0,0),(10,0),(5,10) would return a
> triangle.
>
> I'd prefer the get method to avoid the conversion of strings to
> java.awt.Shape if possible and I thought that MessageBodyReader could
> be used to do that.
>
> I'm I wrong about the use of MessageBodyReader? If so, is there
> another way of accomplishing this?
>
> [1] http://docs.sun.com/app/docs/doc/820-4867/ggqrs?l=en&a=view
>
> On Thu, Aug 21, 2008 at 3:47 PM, Arul Dhesiaseelan
> <arul_at_fluxcorp.com> wrote:
>> If you are using query params in GET, then I would think that you
>> need to
>> provide a String type instead of a complex type. Change it to
>> String s, it
>> should work and you can invoke using GET http://localhost:8080/?
>> s=shape .
>> Not sure if this is what you intended.
>>
>> -Arul
>>
>> Kem Elbrader wrote:
>>>
>>> I'm getting the following exception when I attempt to deploy my
>>> jersey
>>> app to glassfish v2.
>>>
>>> com.sun.jersey.api.container.ContainerException: Method, public
>>> java.awt.image.BufferedImage
>>> com.el.ui.graphicserver.Renderer.draw(java.awt.Shape), annotated
>>> with
>>> GET of resource, class com.el.ui.graphicserver.Renderer, is not
>>> recognized as valid Java method annotated with @HttpMethod.
>>>
>>> I'm attempting to use the a MessageBodyReader named ShapeProvider to
>>> allow java.awt.Shape to be used as a parameter in my @GET method.
>>> I'm
>>> not sure if I'm using this correctly.
>>>
>>> I've attached my project.
>>>
>>> Thanks for any help.
>>> Kem Elbrader
>>>
>>>
>>> ________________________________
>>> Scanned by MessageLabs for Flux
>>> ________________________________
>>> --------------------------------------------------------------------
>>> ----
>>>
>>> This body part will be downloaded on demand.
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>