Java Reference
In-Depth Information
Once you have this mapping, you can create
Product
objects in your application, return
them, and use them as parameters in JAX-RS resource methods. The JAX-RS runtime
uses JAXB to convert the XML data from the request into a
Product
object and to con-
vert a
Product
object into XML data for the response. The following resource class
provides a simple example:
@Path("/product")
public class ProductService {
@GET
@Path("/get")
@Produces("application/xml")
public Product getProduct() {
Product prod = new Product();
prod.setId(1);
prod.setName("Mattress");
prod.setDescription("Queen size mattress");
prod.setPrice(500);
return prod;
}
@POST
@Path("/create")
@Consumes("application/xml")
public Response createProduct(Product prod) {
// Process or store the product and return a response
// ...
}
}
Some IDEs, such as NetBeans IDE, will run the schema generator tool automatically dur-
ing the build process if you add Java classes that have JAXB annotations to your project.
customer
example contains a more complex relationship between the Java classes that
model the data, which results in a more hierarchical XML representation.
Starting from an Existing XML Schema Definition
If you already have an XML schema definition in an
.xsd
file for the data you want to
expose, use the JAXB schema compiler tool. Consider this simple example of an
.xsd
file: