April 12, 2013

Customizing EclipseLink JPA-RS Messages with MOXy

In a previous post I covered how EclipseLink JPA-RS can be used to expose a JPA persistence unit as a RESTful service.  In that example we interacted with the default message formats.  Since JPA-RS leverages MOXy for its XML and JSON binding we can use MOXy to customize the messages.  In this post I will demonstrate how this is done.



MOXy Metadata (oxm.xml)

We could apply the MOXy metadata via annotations directly on the JPA entities, but in this post we will leverage MOXy's external mapping document.  In the mapping document we will specify an ordering for our properties (line 7), and apply path based mapping (lines 9-16).

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="org.example">
    <java-types>
        <java-type name="Customer">
            <xml-type prop-order="id firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" 
                    xml-path="personal-info/first-name/text()"/>
                <xml-element java-attribute="lastName"  
                    xml-path="personal-info/last-name/text()"/>
                <xml-element java-attribute="address" 
                    xml-path="contact-info/address"/>
                <xml-element java-attribute="phoneNumbers" 
                    xml-path="contact-info/phone-numbers"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>


Referencing the MOXy Metadata (persistence.xml)

When using JPA-RS we can reference our MOXy external mapping document using the eclipselink.jpa-rs.oxm property in the JPA persistence.xml file (line 21).

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="CustomerService" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>CustomerService</jta-data-source>
        <class>org.example.Customer</class>
        <class>org.example.Address</class>
        <class>org.example.PhoneNumber</class>
        <properties>
            <property name="eclipselink.target-database" value="Oracle" />
            <property name="eclipselink.logging.level" value="FINEST" />
            <property name="eclipselink.logging.level.ejb_or_metadata" value="WARNING" />
            <property name="eclipselink.logging.timestamp" value="false"/>
            <property name="eclipselink.logging.thread" value="false"/>
            <property name="eclipselink.logging.session" value="false"/>
            <property name="eclipselink.logging.exceptions" value="false"/>
            <property name="eclipselink.target-server" value="SunAS9"/>
            <property name="eclipselink.jpa-rs.oxm" value="org/example/oxm.xml"/>
        </properties>
    </persistence-unit>
</persistence>

Persisting an Entity

We will use the POST operation to create a new instance of the Customer entity.

POST

The URI for our POST operation remains the same as before we customized the MOXy metadata (see Introducing EclipseLink JPA-RS).

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer

Request 

The data we send has been changed to match the new mapping metadata.  Now data is nested within the personal-info (line 3) and contact-info (line 7) nodes.

{
    "id" : 1,
    "personal-info" : {
        "firstName" : "Jane",
        "lastName" : "Doe"
    },
    "contact-info" : {
        "address" : {
            "id" : 1,
            "street" : "1 A Street",
            "city" : "Any Town"
        },
        "phone-numbers" : [{
            "id" : 2,
            "type" : "work",
            "num" : "555-1111"
         }, {
            "id" : 3,
            "type" : "home",
            "num" : "555-2222"
        }]
    }
}

Performing a Query

JPA-RS automatically creates URIs for each of the named queries that we defined in our JPA model:

GET

The URI for our GET operation remains the same as before we customized the MOXy metadata (see Introducing EclipseLink JPA-RS).

http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer

Response

The data we receive has changed to match the new mapping metadata.  The order of the properties has changed, and now data is nested within the personal-info (line 4) and contact-info (line 22) nodes. 
[
    {
        "id": 1,
        "personal-info": {
            "first-name": "Jane",
            "last-name": "Doe"
        },
        "_relationships": [
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/address",
                    "rel": "address"
                }
            },
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/phoneNumbers",
                    "rel": "phoneNumbers"
                }
            }
        ],
        "contact-info": {
            "address": {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Address/1",
                    "method": "GET",
                    "rel": "self"
                }
            },
            "phone-numbers": [
                {
                    "_link": {
                        "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/3",
                        "method": "GET",
                        "rel": "self"
                    }
                },
                {
                    "_link": {
                        "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/2",
                        "method": "GET",
                        "rel": "self"
                    }
                }
            ]
        }
    }
]

Further Reading 

If you enjoyed this post then you may also be interested in:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.