Java Reference
In-Depth Information
schema and your database schema. The best way to avoid this problem is to create two separ-
ate class hierarchies. That way, your XML and database mappings can evolve separately
from one another. Yes, it's a little more code for you to write, but it will save you headaches
in the long run.
I'm going to skip a lot of the details of this example. You've already seen how JAXB classes
work and this topic isn't an exercise on learning JPA, so I'll focus on how JAX-RS interacts
with EJB. Let's take a look at one of the EJBs:
ejb/src/main/java/com/restfully/shop/services/CustomerResource.java
@Path
(
"/customers"
)
public
public interface
interface
CustomerResource
CustomerResource
{
@POST
@Consumes
(
"application/xml"
)
Response
createCustomer
(
Customer customer
,
@Context
UriInfo uriInfo
);
@GET
@Produces
(
"application/xml"
)
@Formatted
Customers
getCustomers
(
@QueryParam
(
"start"
)
int
int
start
,
@QueryParam
(
"size"
)
@DefaultValue
(
"2"
)
int
int
size
,
@QueryParam
(
"firstName"
)
String firstName
,
@QueryParam
(
"lastName"
)
String lastName
,
@Context
UriInfo uriInfo
);
@GET
@Path
(
"{id}"
)
@Produces
(
"application/xml)
Customer getCustomer(@PathParam("
id
"
)
int
int
id
);
}
For a non-JAX-RS-aware EJB container to work with JAX-RS, you need to define your
JAX-RS annotations on the EJB's business interface. The
CustomerResource
interface does
just this.
Our EJB business logic is defined within the
CustomerResourceBean
class:
ejb/src/main/java/com/restfully/shop/services/CustomerResourceBean.java
@Stateless
public
public class
class
CustomerResourceBean
CustomerResourceBean
implements
implements
CustomerResource
{