Java Reference
In-Depth Information
}
}
In this code snippet, the
getEmployee
method is the subresource locator that provides
the
Employee
object, which services requests for
lastname
.
If your HTTP request is
GET /employeeinfo/employees/as209/
, the
getEm-
ployee
method returns an
Employee
object whose id is
as209
. At runtime, JAX-
RS sends a
GET /employeeinfo/employees/as209/lastname
request to the
getEmployeeLastName
method. The
getEmployeeLastName
method retrieves
and returns the last name of the employee whose id is
as209
.
Integrating JAX-RS with EJB Technology and CDI
JAX-RS works with Enterprise JavaBeans technology (enterprise beans) and Contexts and
Dependency Injection for the Java EE Platform (CDI).
In general, for JAX-RS to work with enterprise beans, you need to annotate the class of a
bean with
@Path
to convert it to a root resource class. You can use the
@Path
annotation
with stateless session beans and singleton POJO beans.
The following code snippet shows a stateless session bean and a singleton bean that have
been converted to JAX-RS root resource classes.
@Stateless
@Path("stateless-bean")
public class StatelessResource {...}
@Singleton
@Path("singleton-bean")
public class SingletonResource {...}
Session beans can also be used for subresources.
JAX-RS and CDI have slightly different component models. By default, JAX-RS root
resource classes are managed in the request scope, and no annotations are required for
specifying the scope. CDI managed beans annotated with
@RequestScoped
or
@Ap-
plicationScoped
can be converted to JAX-RS resource classes.
The following code snippet shows a JAX-RS resource class.
@Path("/employee/{id}")
public class Employee {
public Employee(@PathParam("id") String id) {...}