Java Reference
In-Depth Information
protected String name;
@XmlElement(required = true)
protected String description;
protected int price;
// Setter and getter methods
// ...
}
You can create instances of the Product class from your application (for example, from
a database). The generated class product.xml.ObjectFactory contains a method
that allows you to convert these objects to JAXB elements that can be returned as XML
inside JAX-RS resource methods:
Click here to view code image
@XmlElementDecl(namespace = "http://xml.product", name = "product")
public JAXBElement<Product> createProduct(Product value) {
return new JAXBElement<Product>(_Product_QNAME, Product.class,
null, value);
}
The following code shows how to use the generated classes to return a JAXB element as
XML in a JAX-RS resource method:
Click here to view code image
@Path("/product")
public class ProductService {
@GET
@Path("/get")
@Produces("application/xml")
public JAXBElement<Product> getProduct() {
Product prod = new Product();
prod.setId(1);
prod.setName("Mattress");
prod.setDescription("Queen size mattress");
prod.setPrice(500);
return new ObjectFactory().createProduct(prod);
}
}
For @POST and @PUT resource methods, you can use a Product object directly as a
parameter. JAX-RS maps the XML data from the request into a Product object.
Search WWH ::




Custom Search