You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sisyphus <in...@shaw.ca> on 2011/02/15 02:50:14 UTC

JAX-RS @GET request with POJO as @QueryParam

Greetings,

I have a JAX-RS resource which is declared as an @GET method.  Two of the
input parameters are POJOs and I am having trouble figuring out what the URI
is to submit the request (using SoapUI to test).

To keep things simple, let us consider the following:

class interface Test {
   @GET
   @Produces("application/xml")
   @Path("/getData")
   public EmployeeDetails getData(@QueryParam("employee") Employee
employee);
}

If the Employee POJO is declared as:

@XmlRootElement(name="employee")
public class Employee {
   private int id;
   private String name;

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

I would expect to be able to submit the following, but I cannot figure out
the exact format:
   http://<end-point>/getData?employee.id=301&employee.name=Bob
or
   http://<end-point>/getData?employee/id/301&employee/name/Bob

Could someone please point out where my mistake is.

Thanks, Ian

-- 
View this message in context: http://cxf.547215.n5.nabble.com/JAX-RS-GET-request-with-POJO-as-QueryParam-tp3385354p3385354.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JAX-RS @GET request with POJO as @QueryParam

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Ian

On Tue, Feb 15, 2011 at 1:50 AM, Sisyphus <in...@shaw.ca> wrote:
>
> Greetings,
>
> I have a JAX-RS resource which is declared as an @GET method.  Two of the
> input parameters are POJOs and I am having trouble figuring out what the URI
> is to submit the request (using SoapUI to test).
>
> To keep things simple, let us consider the following:
>
> class interface Test {
>   @GET
>   @Produces("application/xml")
>   @Path("/getData")
>   public EmployeeDetails getData(@QueryParam("employee") Employee
> employee);
> }
>

Please check

http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-DealingwithParameters.

@QueryParam("employee") will only pickup a single (or a list of) query
pair with the name 'employer', ex, ?employer=Fred. And if you'd like
the runtime to populate an Employee instance then the Employee class
should a factory constructor or method like valueOf, accepting String.
If not then a ParameterHandler can be registered.

However, it can only help with populating Employee completely if the
'employer' has all the info needed for a custom parser, ex, using a
FIQL like language: employee=name==Fred,id==123.

So  the alternative is to introduce two QueryParams, one for the name,
one for the id,  which will break once you decide to add an address or
something.

However, having getData(QueryParam("") Employer e)

will ensure that  given a query like ?name=Fred&id=123,
the Employer instance will be populated.

?name=Fred&id=123&address.city=Boston,

and if you have a Employer Address property then you'll get the
internal Employer Address populated too.
This is something I hope we can extend the FIQL parser with too, so
that we can introduce a bit of a structure into the fiql queries, just
fyi :

http://cxf.apache.org/docs/jax-rs-advanced-features.html#JAX-RSAdvancedFeatures-FIQLsearchqueries

Hope that helps
Sergey



> If the Employee POJO is declared as:
>
> @XmlRootElement(name="employee")
> public class Employee {
>   private int id;
>   private String name;
>
>   public int getId() {
>      return id;
>   }
>
>   public void setId(int id) {
>      this.id = id;
>   }
>
>   public String getName() {
>      return name;
>   }
>
>   public void setName(String name) {
>      this.name = name;
>   }
> }
>
> I would expect to be able to submit the following, but I cannot figure out
> the exact format:
>   http://<end-point>/getData?employee.id=301&employee.name=Bob
> or
>   http://<end-point>/getData?employee/id/301&employee/name/Bob
>
> Could someone please point out where my mistake is.
>
> Thanks, Ian
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/JAX-RS-GET-request-with-POJO-as-QueryParam-tp3385354p3385354.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>