users@jersey.java.net

Re: [Jersey] Using annotated POJOs to build client request

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 24 Mar 2009 11:33:55 +0100

Hi Patrick,

Unfortunately this functionality is not currently available.

I agree that is an interesting idea and probably not too difficult to
implement if using the Java Bean API.

The names of the properties could correspond to the template names. No
annotation would be required for such support. One would just iterate
through the properties of the Java bean to create a map of name and
values then build the URI using that map. See below for a very simple
proof of concept.

Paul.

import com.sun.jersey.api.uri.UriTemplate;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;

public class Main {

     public static class A {
         private String a;
         private int b;

         public A(String a, int b) {
             this.a = a;
             this.b = b;
         }

         public String getA() {
             return a;
         }

         public int getB() {
             return b;
         }
     }


     public static void main(String[] args) throws Exception {

         A a = new A("foo", 42);

         BeanInfo bi = Introspector.getBeanInfo(A.class);
         PropertyDescriptor[] pds = bi.getPropertyDescriptors();

         Map<String, String> values = new HashMap<String, String>();
         for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
             String name = pd.getName();
             Object value = pd.getReadMethod().invoke(a);
             values.put(name, value.toString());
         }

         UriTemplate ut = new UriTemplate("http://www.myurl.net/getquote.asp?a=
{a}&b={b}");
         String u = ut.createURI(values);
         System.out.println(u);
     }

}


On Mar 23, 2009, at 7:26 PM, Patrick Sansoucy wrote:

> Hello,
>
> I am quite new to Jersey and the JSR311 spec and was
> looking into using this for a project I am currently working on ...
> I have to call a RESTful service which has a more than 10 request
> parameters which will return a XML response. I was wondering if it
> was possible to use an annoted POJO to build the URI ?
>
>
> For example, something that would look like this ...
>
> http://www.myurl.net/getquote.asp?acct=XXX&type=1&pid=XXX&a=1
>
> And the associated POJO ...
>
> @Path("getquote.asp?
> acct={accountCode}&type={productType}&pid={productIdentifier}")
>
> public class Quote {
>
> /*
> * account code Format: up to 12 alphanumeric characters. Mandatory
> */
> protected String accountCode;
>
> /*
> * product type. Mandatory
> */
> protected int productType;
>
> /*
> * unique product identifier Format: positive integer from result set
> * Mandatory
> */
> protected int productIdentifier;
>
> public void setAccountCode(@PathParam("accountCode") String
> aAccountCode) {
> this.accountCode = aAccountCode;
> }
>
> public String getAccountCode() {
> return this.accountCode;
> }
>
> public int getProductType() {
> return this.productType;
> }
>
> public void setProductType(@PathParam("productType") int
> aProductType) {
> this.productType = aProductType;
> }
>
> public int getProductIdentifier() {
> return this.productIdentifier;
> }
>
> public void setProductIdentifier(@PathParam("productIdentifier") int
> aProductIdentifier) {
> this.productIdentifier = aProductIdentifier;
> }
>
> }
>
> Thank you
> Patrick Sansoucy