On Feb 5, 2009, at 1:22 PM, Wilhelmsen Tor Iver wrote:
> Does UriInfo.getMatchedResources() resolve correctly? I am trying to
> pick up the parent in a sub-resource and only get an uninitialized
> bean back.
>
> Code extract:
>
> @Path("/employee/{id}")
> @Produces("application/xml")
> public class Employee extends EmployeeBean {
>
> @GET
> public Response get(@PathParam("id") String id, @Context
> UriInfo info) {
> ...
>
> @Path("vacationdays")
> public VacationDays getVacationDays() {
> return new VacationDays();
> }
>
>
> public class VacationDays extends VacationDaysBean {
>
> @GET
> public Response get(@Context UriInfo info) {
> List<Object> res = info.getMatchedResources();
> VacationDaysBean ret = null;
> if (res != null && res.size() > 0) {
> Object obj = res.get(res.size() - 1);
> *** if (obj instanceof Employee) {
>
> At the point in the code marked with *** above, obj exists but all
> fields are null, the get() has not been called.
>
What "fields" are you referring to? fields on VacationDays or fields
on Employee? or do you mean method parameters?
What "get()" method are you referring to? Employee.get() or
VacationDays.get()?
I presume your URI path is something like "/employee/1/vacationdays"
thus the Employee.get() will never be called because the URI does not
point to the correct resource it points to the vacation days resource.
In the method Employee.getVacationDays() do you want to pass the value
of the "id" path param to VacationDays?
There are two ways you can do this, by yourself:
1) in the constructor of the VacationDays; or
2) by reusing @PathParam in the VacationDays.get method:
@GET
public Response get(@PathParam("id") String id, @Context
UriInfo info) {
In sub-resource locators it is the responsibility of the application
to instantiate a resource class. Jersey will not inject onto
constructors or fields of the instance returned. There are two Jersey
specific features you can use to ensure that Jersey injects onto
constructors or fields:
1) Return a class e.g. return VacationDays.class
public Class<VacationDays> getVacationDays() {
return VacationDays.class
}
2) Or inject ResourceContext to get an instance:
public VacationDays getVacationDays(@Context ResourceContext
rc) {
return rc.getResource(VacationDays.class);
}
Hope this helps,
Paul.
> Am I doing something wrong, or is it not fully implemented yet?
> Would it work if I added a constructor with a @PathParm as well? Or
> do I need to use a different getter method?
>
> Med vennlig hilsen
>
> TOR IVER WILHELMSEN
> Senior systemutvikler
> Arrive AS
> T (+47) 48 16 06 18
> E-post: toriverw_at_arrive.no
> http://www.arrive.no
> http://servicedesk.arrive.no
>
>