Java Reference
In-Depth Information
For per-request resources, you may alternatively use these injection annotations on the fields,
setter methods, and even constructor parameters of your JAX-RS resource class. Do not try
to use these annotations on fields or setter methods if your component model does not follow
per-request instantiation. Singletons process HTTP requests concurrently, so it is not possible
to use these annotations on fields or setter methods, as concurrent requests will overrun and
conflict with each other.
@PathParam
We looked at @javax.ws.rs.PathParam a little bit in Chapters 3 and 4 . @PathParam allows
you to inject the value of named URI path parameters that were defined in @Path expres-
sions. Let's revisit the CustomerResource example that we defined in Chapter 2 and imple-
mented in Chapter 3 :
@Path ( "/customers" )
public
public class
class CustomerResource
CustomerResource {
...
@Path ( "{id}" )
@GET
@Produces ( "application/xml" )
public
public StreamingOutput getCustomer ( @ PathParam ( "id" ) int id ) {
...
}
}
More Than One Path Parameter
You can reference more than one URI path parameter in your Java methods. For instance,
let's say we are using first and last name to identify a customer in our CustomerResource :
@Path ( "/customers" )
public
public class
class CustomerResource
CustomerResource {
...
@Path ( "{first}-{last}" )
@GET
@Produces ( "application/xml" )
public
public StreamingOutput getCustomer ( @PathParam ( "first" ) String firstName ,
@PathParam ( "last" ) String lastName ) {
...
}
}
Search WWH ::




Custom Search