Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    http://glassfish.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->
<html><head><title>MOXy Example</title></head>


<body>
<h1>MOXy Example</h1>

<h2>What is MOXy</h2>
<p>This example demonstrates that you can utilize MOXy extensions when dealing with XML representation when developing a Jersey based RESTful Web application</p>

<p>MOXy is EclipseLink's Object to XML Mapping services. MOXy allows for a POJO object model to be mapped to an XML schema. The Java Architecture for XML Binding (JAXB) provides a Java standard for object XML mapping (OXM). MOXy supports JAXB, as well as providing its' own native API and integration with Web Services.
</p>

<h2>MOXy XML Path Mapping Extension</h2>

<p>The MOXy extension shown in this example is described on the Eclipse Wiki site
    at <a href="http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions">http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions</a>.
    It allows you to specify path based mapping via @XmlPath annotation.
</p>

<p>If the MOXy extension was not used, the XML represenation of the customer data would look like follows:
      <pre>
&lt;customer&gt;
  &lt;name&gt;Jane Doe&lt;/name&gt;
  &lt;address&gt;
    &lt;city&gt;My Town&lt;/city&gt;
    &lt;street&gt;123 Any Street&lt;/street&gt;
  &lt;/address&gt;
  &lt;phoneNumbers type="work"&gt;613-555-1111&lt;/phoneNumbers&gt;
  &lt;phoneNumbers type="cell"&gt;613-555-2222&lt;/phoneNumbers&gt;
&lt;/customer&gt;
      </pre>
By adding <code>@org.eclipse.persistence.oxm.annotations.XmlPath</code> annotation to the bean definition classes,
you will get the following XML representation instead:
      <pre>
&lt;customer&gt;
  <b>&lt;personal-info&gt;</b>
    &lt;name&gt;Jane Doe&lt;/name&gt;
  <b>&lt;/personal-info&gt;
      &lt;contact-info&gt;</b>
    &lt;address&gt;
      &lt;city&gt;My Town&lt;/city&gt;
      &lt;street&gt;123 Any Street&lt;/street&gt;
    &lt;/address&gt;
    <b>&lt;phone-number type="work"&gt;</b>613-555-1111<b>&lt;/phone-number&gt;</b>
    <b>&lt;phone-number type="cell"&gt;</b>613-555-2222<b>&lt;/phone-number&gt;</b>
  <b>&lt;/contact-info&gt;</b>
&lt;/customer&gt;
      </pre>
XML Path expressions used are:
<ul>
    <li>personal-info/name/text()</li>
    <li>contact-info/address</li>
    <li>contact-info/phone-number</li>
</ul>
Please check out the source code and the wiki page linked above for the detailed information on the XML Path mapping feature.
</p>

<h2>Replacing Implicit JAXB Runtime With MOXy</h2>
<p>Since MOXy is a JAXB implementation, the example still utilizes the standard Jersey JAXB message body reader/writer providers.
    To make Jersey use MOXy runtime, you just need to put a <code>jaxb.properties</code> file into the Java package
    containing your JAXB beans. The file should have the following content:
<pre>
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
</pre>
Concrete file used in this example is placed under <code>src/main/resources/com/sun/jersey/samples/moxy/beans/jaxb.properties</code>
</p>
<h2>REST Resources</h2>
<p>The example consists of a single REST Resource represented by the following Java class:</p>
<dl>
    <dt><code>org.glassfish.jersey.examples.xmlmoxy.CustomerResource</code></dt>
    <dd>A resource class that maintains a single customer data.</dd>
</dl>
<p>The mapping of the URI path space is presented in the following table:</p>
<table border="1">
    <tr>
        <th>URI path</th>
        <th>Resource class</th>
        <th>HTTP methods</th>
    </tr>
    <tr>
        <td>/customer</td>
        <td>CustomerResource</td>
        <td>GET</td>
    </tr>
    <tr>
        <td>/customer</td>
        <td>CustomerResource</td>
        <td>PUT</td>
    </tr>
</table>

<h2>Running the Example</h2>

<blockquote><code>mvn clean compile exec:java</code></blockquote>
<p>From a web browser, visit:</p>
<blockquote><code><a href="http://localhost:8080/xml-moxy/customer">http://localhost:8080/moxy/customer</a></code>
</blockquote>
</body></html>