Enterprise Java Development@TOPIC@
Elements
Attributes
Namespaces
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:catageory name="snacks" id="1" version="1" xmlns:ns2="http://webejb.ejava.info/inventory">
<productCount>1</productCount>
<products>
<product id="1" name="chips" version="0"/>
</products>
</ns2:catageory>
@XmlRootElement - needed to be able to manipulate object independently of graph
@XmlType - defines/names structure of XML construct
@XmlAccessorType - directs JAXB to invoke property or field accessors
@XmlRootElement(name="catageory", namespace=InventoryRepresentation.NAMESPACE)
@XmlType(name="Category", namespace=InventoryRepresentation.NAMESPACE)
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Category extends InventoryRepresentation {
private int id;
private String name;
private Integer productCount;
private List<Product> products=new ArrayList<Product>();
public Category() {}
public Category(String name) {
this.name=name;
}
...
}
Typically used for identifying properties
Single value, non-repeating
@XmlAttribute(required=true)
public int getId() { return id; }
public void setId(int id) {
this.id = id;
}
@XmlAttribute(required=true)
public String getName() { return name; }
public void setName(String name) {
this.name = name;
}
Typically used for descriptive properties
@XmlElement(required=true)
public int getProductCount() { return productCount!=null ? productCount :products.size();}
public void setProductCount(int productCount) {
this.productCount = productCount;
}
JAXBContext
Marshaller
JAXB_FORMATTED_OUTPUT
JAXBContext jbx = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jbx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(object, writer);
return writer.toString();
JAX-RS requires built-in provider support for JAXB
JAX-RS defines marshalling framework to add extensions
Client request payload demarshalled into input JAXB class
@POST @Path("")
@Consumes(MediaType.APPLICATION_XML)
public Response addCustomer(Customer customer) {