EntityToJAXBMappingForREST

Entity To JAXB and REST Mapping

  • Given a persistence entity class, we can directly add a type annotation @XmlRootElement and make it available for use by JAXBRepresentation.
  • For container of this entity, we can simple codegen a JAXB class as following:
@XmlRootElement
public class Products {
    private List<Product> products;
    // . . . . . 
    public void setProduct(List<Product> products) {
        this.products = products;
    }
    public List<Product> getProduct() {
        return products;
    }
}
  • For content of representation with relationship to other entity, we would need to support both choice for representing the related entity:
  • a resource URI that would return the related entity, this will avoid cycle error by JAXB marshaling code.
  • plain in-line XML; for this we have to let decide based on: (1) how large is the data of the related entity, (2) is the related entity has possibility of circular references.
  • To return a link, we will need to annotate @XmlTransient the method for the relationship, and also add a @javax.persistence.Transient private field for the computed value of the link.
  • To check out the modified sample project at prototypes/jpa-jaxb/SimpleServlet use:
    CVSROOT=:pserver:nn136682@daning.sfbay.sun.com:/cvs/sandbox.  
  • Deploy and run it using Sun JSAS you will see the browser with resource URL /customer displaying all the customers in IDE Java DB.
  • You can use this sample to investigate create, update, delete.

-- Main.NamNguyen - 08 Mar 2007

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo