Hi Pat

On 22/11/11 14:53, Pat Turner wrote:
Hi Sergey,

I have two root resources: Fleet and Bus. I want to be able to list all
Buses or all Busses by Fleet. I had expected to expose URLs such as:

#All Busses
/busses
# Busses by Fleet
/fleets/{fleet_id}/busses

My Bus resource looks like this:

@Path("/busses")
@Component
public class BusResource {

        @PathParam(fleet_id)
        private String fleetId;

        public List<Bus>  getBusses() {
                //sercice call: lookup busses for fleetId, or all if none 
provided
        }
}


My Fleet resource looks like this:

@Path("/fleets")
@Component
public class FleetResource {

        //TODO inject/lookup with jax-rs context resources
        @Autowired
        BusResource busResource

        @Path("/{fleet_id}/busses")
        public Object getBusses() {
                return busResource
        }
}

I realise I'm actually (ab)using a root resource as a sub-resource, but I
can't see a nice way to use the same code to access resources with and
without context (Fleet).

Reusing a BusResource seems completely perfect to me, I'd do it the similar way, but I believe

>    @PathParam(fleet_id)
>    private String fleetId;

is just not thread-safe, unless you get Spring injecting a new BusResource instance into FleetResource per request (with request scope may be).

I'd just update it like this:

@Path("/busses")
@Component
public class BusResource {

        
        @GET
        public List<Bus>  getBusses(@PathParam("fleetid") String id) {
                        
        }
}

@Path("/fleets")
@Component
public class FleetResource {

        //TODO inject/lookup with jax-rs context resources
        @Autowired
        BusResource busResource

        @Path("/{fleet_id}/busses")
        public BusResource getBusses() {
                return busResource;
        }
}

so /busses/1 and /fleets/1/busses will work equally well, all is thread-safe, and even cxf jaxrs proxies will be able to do

FleetResource fleet = JAXRSClientFactory.create(FleetResource.class);
List<BusResource> bus = fleet.getBusses().getBusses("1");

I can look into the runtime supporting the injection into subresources, but that will probably require setting a property which will indicate to the runtime that the injection is safe

Cheers, Sergey


Thanks again,
Pat

--
View this message in context: 
http://cxf.547215.n5.nabble.com/Injecting-jax-rs-PathParam-into-sub-resource-fields-tp5013364p5013741.html
Sent from the cxf-user mailing list archive at Nabble.com.


--
Sergey Beryozkin

http://sberyozkin.blogspot.com

Talend Community Coders
http://coders.talend.com/

Reply via email to