Getting a Good REST (Service) on the Run(time) Shaun Smith Principal Product Manager, Oracle TopLink EclipseLink and Gemini Committer
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
Use Case – Data Access Service In Java EE: Framework  - Java API for RESTful Services (JAX-RS)   Message Binding  -  Java Architecture for XML Binding (JAXB) Persistence  - Java Persistence API (JPA) Data Access Service Data Source XML JSON Objects JPA JAXB JAXB
What is REST? REST  –  RE presentational  S tate  T ransfer Priniciples: Addressable resources (URI per resource) Small set of well-defined methods (i.e. GET, PUT, POST, DELETE) Representation-oriented Communicate statelessly
What is JAX-RS? JAX-RS  - Java API for RESTful Services  Principles Java EE framework for implementing RESTful services Provides annotations to bind combination of URI and HTTP operation to Java methods. Specifications JAX-RS 1.0 (JSR 311) – Released October 2008 JAX-RS 2.0 (JSR 339) – In Progress
REST Service – Read (GET) REST GET operations correspond to JPA queries.  @GET @Path(&quot;{id}&quot;) @Produces({&quot;application/xml&quot;, &quot;application/json&quot;}) public Customer read(@PathParam(&quot;id&quot;) int id) { return entityManager.find(Customer.class, id); } Relational Database JAX-RS EclipseLink JPA EclipseLink JAXB XML Document
REST Service – Create (POST) REST POST operations correspond to JPA persist  operations.  @POST @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) public void create(Customer customer) { entityManager.persist(customer); }
REST Service – Update (PUT) REST PUT operations correspond to JPA merge  operations.  @PUT @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) public void update(Customer customer) { entityManager.merge(customer); }
REST Service – Delete (DELETE) REST DELETE operations correspond to JPA remove  operations.  @DELETE @Path(&quot;{id}&quot;) public void delete(int id) { Customer customer = entityManager.find(Customer.class, id); if(null != customer) { entityManager.remove(entity); } }
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
Pain Points – Mapping JPA Entities to XML Bidirectional/Cyclical Relationships  Composite Keys/Embedded Key Classes
Pain Points – Bidirectional Relationships JAXB Standard JAXB does not support bidirectional relationships.  One side must be marked  @XmlTransient . JAXB implementations may provide extensions for handling bidirectional relationships such as  @XmlInverseReference(mappedBy=“foo”)  in MOXy.
Pain Points – Shared References Privately owned references can be represented through key/foreign key relationships in XML. Use @XmlID/@XmlIDREF.
Pain Points – Embedded Key Classes Standard JAXB does not support embedded key classes. JAXB implementations such as EclipseLink MOXy offer extensions to handle this use case (mapping is the same as for composite keys).
Pain Points – Composite Keys Standard JAXB does not support composite keys. Sample extension, EclipseLink JAXB (MOXy): @XmlJoinNodes({   @XmlJoinNode(xmlPath=&quot;address/@id&quot;,    referencedXmlPath=&quot;@id&quot;),   @XmlJoinNode(xmlPath=“address/@city&quot;,    referencedXmlPath=&quot;city/text()&quot;) }) public Address address;
For More Information EclipseLink http://www.eclipse.org/eclipselink/ http://wiki.eclipse.org/EclipseLink bdoughan.blogspot.com
 

RESTful Data Access Services with Java EE

  • 1.
    Getting a GoodREST (Service) on the Run(time) Shaun Smith Principal Product Manager, Oracle TopLink EclipseLink and Gemini Committer
  • 2.
    Java Persistence: TheProblem 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.
    Use Case –Data Access Service In Java EE: Framework - Java API for RESTful Services (JAX-RS)   Message Binding - Java Architecture for XML Binding (JAXB) Persistence - Java Persistence API (JPA) Data Access Service Data Source XML JSON Objects JPA JAXB JAXB
  • 4.
    What is REST?REST – RE presentational S tate T ransfer Priniciples: Addressable resources (URI per resource) Small set of well-defined methods (i.e. GET, PUT, POST, DELETE) Representation-oriented Communicate statelessly
  • 5.
    What is JAX-RS?JAX-RS - Java API for RESTful Services Principles Java EE framework for implementing RESTful services Provides annotations to bind combination of URI and HTTP operation to Java methods. Specifications JAX-RS 1.0 (JSR 311) – Released October 2008 JAX-RS 2.0 (JSR 339) – In Progress
  • 6.
    REST Service –Read (GET) REST GET operations correspond to JPA queries. @GET @Path(&quot;{id}&quot;) @Produces({&quot;application/xml&quot;, &quot;application/json&quot;}) public Customer read(@PathParam(&quot;id&quot;) int id) { return entityManager.find(Customer.class, id); } Relational Database JAX-RS EclipseLink JPA EclipseLink JAXB XML Document
  • 7.
    REST Service –Create (POST) REST POST operations correspond to JPA persist operations. @POST @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) public void create(Customer customer) { entityManager.persist(customer); }
  • 8.
    REST Service –Update (PUT) REST PUT operations correspond to JPA merge operations. @PUT @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;}) public void update(Customer customer) { entityManager.merge(customer); }
  • 9.
    REST Service –Delete (DELETE) REST DELETE operations correspond to JPA remove operations. @DELETE @Path(&quot;{id}&quot;) public void delete(int id) { Customer customer = entityManager.find(Customer.class, id); if(null != customer) { entityManager.remove(entity); } }
  • 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.
    Pain Points –Mapping JPA Entities to XML Bidirectional/Cyclical Relationships Composite Keys/Embedded Key Classes
  • 12.
    Pain Points –Bidirectional Relationships JAXB Standard JAXB does not support bidirectional relationships. One side must be marked @XmlTransient . JAXB implementations may provide extensions for handling bidirectional relationships such as @XmlInverseReference(mappedBy=“foo”) in MOXy.
  • 13.
    Pain Points –Shared References Privately owned references can be represented through key/foreign key relationships in XML. Use @XmlID/@XmlIDREF.
  • 14.
    Pain Points –Embedded Key Classes Standard JAXB does not support embedded key classes. JAXB implementations such as EclipseLink MOXy offer extensions to handle this use case (mapping is the same as for composite keys).
  • 15.
    Pain Points –Composite Keys Standard JAXB does not support composite keys. Sample extension, EclipseLink JAXB (MOXy): @XmlJoinNodes({ @XmlJoinNode(xmlPath=&quot;address/@id&quot;, referencedXmlPath=&quot;@id&quot;), @XmlJoinNode(xmlPath=“address/@city&quot;, referencedXmlPath=&quot;city/text()&quot;) }) public Address address;
  • 16.
    For More InformationEclipseLink http://www.eclipse.org/eclipselink/ http://wiki.eclipse.org/EclipseLink bdoughan.blogspot.com
  • 17.