Your understanding is basically correct. JAXB and JAX-RS are both Java Community Process (JCP) standards with multiple implementations.
JAXB - Defines standardized metadata and runtime API for converting Java domain objects to/from XML.
JAX-RS - Defines standardized metadata and runtime API for the creation of RESTful services. By default for the application/xml
media type JAX-RS will use JAXB to convert the objects to/from XML.
Example
In the following example when a GET
operation is performed the JAX-RS implementation will return a Customer
. A JAXB implementation will be used to convert that instance of Customer
to the XML that the client will actually receive.
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/customers")
public class CustomerResource {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{id}")
public Customer read(@PathParam("id") int id) {
Customer customer = new Customer();
customer.setId(id);
customer.setFirstName("Jane");
customer.setLastName(null);
PhoneNumber pn = new PhoneNumber();
pn.setType("work");
pn.setValue("5551111");
customer.getPhoneNumbers().add(pn);
return customer;
}
}