Java Reference
In-Depth Information
The Product class represents a basic item in the catalog that users want to view. The Pro-
ductCatalog class consists of a few items of this type. There are only two things required
here. You need this class to conform to the JavaBean conventions so that JAXB can marshal
instances of it into XML for the servlet response. Then you need to add the @XmlType annota-
tion above the type declaration as a hint to JAXB. In the real world, you would likely have
some other mechanism for getting your products represented as XML; that's not the point of
this example. What you care about here is the servlet and how it returns the response, so let's
take only a precursory look at the ProductCatalog class ( Example 8-2 ) .
Example8-2.ProductCatalog.java
package com.soacookbook;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ProductCatalog {
private List<Product> products;
public ProductCatalog() {
products = new ArrayList<Product>();
Product p = new Product();
p.setId("123");
p.setName("Shirt");
p.setPrice(159.95D);
Product p2 = new Product();
p2.setId("456");
p2.setName("Monkey");
p2.setPrice(2500D);
products.add(p);
products.add(p2);
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
Search WWH ::




Custom Search