RESTful services with JAXB and JPA


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Java Applications need to either access existing data sources like relational database or interact with other systems via XML—or BOTH. The example is a trivial one but it can get very complicated to map between an Java object model and a relational or XML schema.
  • RESTful services with JAXB and JPA

    1. 1. Getting a Good REST (Service) on the Run(time) Shaun Smith Principal Product Manager, Oracle TopLink EclipseLink and Gemini Committer
    2. 2. Java Persistence: The Problem Space JAXB : Java Architecture for XML Binding <customer id=“…”> <name>…</name> … </contact-info> </customer> XML JPA : Java Persistence API CUST ID NAME C_RATING Relational Customer id: int name: String creditRating: int Java JPA JAXB DBWS
    3. 3. Use Case – Data Access Service <ul><li>In Java EE: </li></ul><ul><li>Framework - Java API for RESTful Services (JAX-RS)   </li></ul><ul><li>Message Binding - Java Architecture for XML Binding (JAXB) </li></ul><ul><li>Persistence - Java Persistence API (JPA) </li></ul>Data Access Service Data Source XML JSON Objects JPA JAXB JAXB
    4. 4. What is REST? <ul><li>REST – RE presentational S tate T ransfer </li></ul><ul><li>Priniciples: </li></ul><ul><ul><li>Addressable resources (URI per resource) </li></ul></ul><ul><ul><li>Small set of well-defined methods (i.e. GET, PUT, POST, DELETE) </li></ul></ul><ul><ul><li>Representation-oriented </li></ul></ul><ul><ul><li>Communicate statelessly </li></ul></ul>
    5. 5. What is JAX-RS? <ul><li>JAX-RS - Java API for RESTful Services </li></ul><ul><li>Principles </li></ul><ul><ul><li>Java EE framework for implementing RESTful services </li></ul></ul><ul><ul><li>Provides annotations to bind combination of URI and HTTP operation to Java methods. </li></ul></ul><ul><li>Specifications </li></ul><ul><ul><li>JAX-RS 1.0 (JSR 311) – Released October 2008 </li></ul></ul><ul><ul><li>JAX-RS 2.0 (JSR 339) – In Progress </li></ul></ul>
    6. 6. REST Service – Read (GET) <ul><li>REST GET operations correspond to JPA queries. </li></ul><ul><li>@GET </li></ul><ul><li>@Path(&quot;{id}&quot;) </li></ul><ul><li>@Produces({&quot;application/xml&quot;, &quot;application/json&quot;}) </li></ul><ul><li>public Customer read(@PathParam(&quot;id&quot;) int id) { </li></ul><ul><li>return entityManager.find(Customer.class, id); </li></ul><ul><li>} </li></ul>Relational Database JAX-RS EclipseLink JPA EclipseLink JAXB XML Document
    7. 7. REST Service – Create (POST) <ul><li>REST POST operations correspond to JPA persist </li></ul><ul><li>operations. </li></ul><ul><li>@POST </li></ul><ul><li>@Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) </li></ul><ul><li>public void create(Customer customer) { </li></ul><ul><li>entityManager.persist(customer); </li></ul><ul><li>} </li></ul>
    8. 8. REST Service – Update (PUT) <ul><li>REST PUT operations correspond to JPA merge </li></ul><ul><li>operations. </li></ul><ul><li>@PUT </li></ul><ul><li>@Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) </li></ul><ul><li>public void update(Customer customer) { </li></ul><ul><li>entityManager.merge(customer); </li></ul><ul><li>} </li></ul>
    9. 9. REST Service – Delete (DELETE) <ul><li>REST DELETE operations correspond to JPA remove </li></ul><ul><li>operations. </li></ul><ul><li>@DELETE </li></ul><ul><li>@Path(&quot;{id}&quot;) </li></ul><ul><li>public void delete(int id) { </li></ul><ul><li>Customer customer </li></ul><ul><li>= entityManager.find(Customer.class, id); </li></ul><ul><li>if(null != customer) { </li></ul><ul><li>entityManager.remove(entity); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    10. 10. Java Persistence API (JPA) & JAXB @XmlRootElement @Entity public class Customer { @XmlAttribute @Id public int getId() {…} public void setId(int id) {…} @XmlElement(name=“billing-address”) @OneToOne @JoinColumn(name=&quot;ADDR_ID&quot;) public Address getBillingAddress() {…} public void setBillingAddress(Address address) {…} } Combining JAXB and JPA Annotations
    11. 11. Pain Points – Mapping JPA Entities to XML <ul><li>Bidirectional/Cyclical Relationships </li></ul><ul><li>Composite Keys/Embedded Key Classes </li></ul>
    12. 12. Pain Points – Bidirectional Relationships <ul><li>JAXB </li></ul><ul><li>Standard JAXB does not support bidirectional relationships. One side must be marked @XmlTransient . </li></ul><ul><li>JAXB implementations may provide extensions for handling bidirectional relationships such as @XmlInverseReference(mappedBy=“foo”) in MOXy. </li></ul>
    13. 13. Pain Points – Shared References <ul><li>Privately owned references can be represented through key/foreign key relationships in XML. </li></ul><ul><li>Use @XmlID/@XmlIDREF. </li></ul>
    14. 14. Pain Points – Embedded Key Classes <ul><li>Standard JAXB does not support embedded key classes. </li></ul><ul><li>JAXB implementations such as EclipseLink MOXy offer extensions to handle this use case (mapping is the same as for composite keys). </li></ul>
    15. 15. Pain Points – Composite Keys <ul><li>Standard JAXB does not support composite keys. </li></ul><ul><li>Sample extension, EclipseLink JAXB (MOXy): </li></ul><ul><ul><li>@XmlJoinNodes({ @XmlJoinNode(xmlPath=&quot;address/@id&quot;, referencedXmlPath=&quot;@id&quot;), @XmlJoinNode(xmlPath=“address/@city&quot;, referencedXmlPath=&quot;city/text()&quot;) }) public Address address; </li></ul></ul>
    16. 16. For More Information <ul><li>EclipseLink </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.