| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<!-- |
| 3 |
|
| 4 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 5 |
|
| 6 |
Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 7 |
|
| 8 |
The contents of this file are subject to the terms of either the GNU |
| 9 |
General Public License Version 2 only ("GPL") or the Common Development |
| 10 |
and Distribution License("CDDL") (collectively, the "License"). You |
| 11 |
may not use this file except in compliance with the License. You can |
| 12 |
obtain a copy of the License at |
| 13 |
http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 14 |
or packager/legal/LICENSE.txt. See the License for the specific |
| 15 |
language governing permissions and limitations under the License. |
| 16 |
|
| 17 |
When distributing the software, include this License Header Notice in each |
| 18 |
file and include the License file at packager/legal/LICENSE.txt. |
| 19 |
|
| 20 |
GPL Classpath Exception: |
| 21 |
Oracle designates this particular file as subject to the "Classpath" |
| 22 |
exception as provided by Oracle in the GPL Version 2 section of the License |
| 23 |
file that accompanied this code. |
| 24 |
|
| 25 |
Modifications: |
| 26 |
If applicable, add the following below the License Header, with the fields |
| 27 |
enclosed by brackets [] replaced by your own identifying information: |
| 28 |
"Portions Copyright [year] [name of copyright owner]" |
| 29 |
|
| 30 |
Contributor(s): |
| 31 |
If you wish your version of this file to be governed by only the CDDL or |
| 32 |
only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 33 |
elects to include this software in this distribution under the [CDDL or GPL |
| 34 |
Version 2] license." If you don't indicate a single choice of license, a |
| 35 |
recipient has the option to distribute your version of this file under |
| 36 |
either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 37 |
its licensees as provided above. However, if you add GPL Version 2 code |
| 38 |
and therefore, elected the GPL Version 2 license, then the option applies |
| 39 |
only if the new code is made subject to such option by the copyright |
| 40 |
holder. |
| 41 |
|
| 42 |
--> |
| 43 |
<html><head><title>MOXy OXM External Mapping Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>MOXy OXM External Mapping Example</h1> |
| 48 |
|
| 49 |
<p>This example demonstrates utilizing MOXy OXM External Mapping extension when dealing with XML representation when developing a Jersey based RESTful Web application</p> |
| 50 |
|
| 51 |
<h2>What is MOXy OXM External Mapping Extension</h2> |
| 52 |
|
| 53 |
<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. |
| 54 |
</p> |
| 55 |
<p>In addition to using Java annotations, EclipseLink provides an XML mapping configuration file called eclipselink-oxm.xml. This mapping file can be used in place of or to override JAXB annotations in source. In addition to allowing all of the standard JAXB mapping capabilities it also includes advanced mapping types and options.</p> |
| 56 |
|
| 57 |
<p>The MOXy extension shown in this example is described on the Eclipse Wiki site |
| 58 |
at <a href="http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML">http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML</a>. |
| 59 |
The extension allows you to process Java beans which do not use JAXB annotations to produce and/or consume XML. To emphasise you do not need access to source code of such beans, the XML Java model classes for this example are placed in a separate module called beans. |
| 60 |
The other module, webapp, then only contains a JAX-RS web resource, CustomerResource, which uses MOXy to serialize customer XML representation. |
| 61 |
</p> |
| 62 |
|
| 63 |
<p>Please check out the source code and the wiki page linked above for the detailed information on the OXM external mapping feature. |
| 64 |
</p> |
| 65 |
|
| 66 |
<h2>Replacing Implicit JAXB Runtime With MOXy</h2> |
| 67 |
<p>Since MOXy is a JAXB implementation, the example still utilizes the standard Jersey JAXB message body reader/writer providers. |
| 68 |
To make Jersey use MOXy runtime, you just need to put a <code>jaxb.properties</code> file into the Java package |
| 69 |
containing your JAXB beans. The file should have the following content: |
| 70 |
<pre> |
| 71 |
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory |
| 72 |
</pre> |
| 73 |
Concrete file used in this example is placed under <code>src/main/resources/com/sun/jersey/samples/moxy/beans/jaxb.properties</code> |
| 74 |
</p> |
| 75 |
<p>So far the configuration was the same as in the other MOXy example in Jersey. |
| 76 |
Now to enable MOXy JAXB provider also for the types, which do not use JAXB annotations, |
| 77 |
you need to name respective Java packages in Jersey ResourceConfig property called <code>com.sun.jersey.moxy.config.property.packages</code>. |
| 78 |
And finally to make Jersey use properly configured JAXB context resolver, you need |
| 79 |
to add |
| 80 |
<code>com.sun.jersey.moxy.MoxyContextResolver</code> to your provider classes. |
| 81 |
In this example we do so via <code>META-INF/services/jersey-server-components</code> |
| 82 |
and <code>META-INF/services/jersey-client-components</code> placed in the web application WAR file. |
| 83 |
</p> |
| 84 |
<h2>REST Resources</h2> |
| 85 |
<p>The example consists of a single REST Resource represented by the following Java class:</p> |
| 86 |
<dl> |
| 87 |
<dt><code>com.sun.jersey.samples.moxy.CustomerResource</code></dt> |
| 88 |
<dd>A resource class that maintains a single customer data.</dd> |
| 89 |
</dl> |
| 90 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 91 |
<table border="1"> |
| 92 |
<tr> |
| 93 |
<th>URI path</th> |
| 94 |
<th>Resource class</th> |
| 95 |
<th>HTTP methods</th> |
| 96 |
</tr> |
| 97 |
<tr> |
| 98 |
<td>/customer</td> |
| 99 |
<td>CustomerResource</td> |
| 100 |
<td>GET</td> |
| 101 |
</tr> |
| 102 |
<tr> |
| 103 |
<td>/customer</td> |
| 104 |
<td>CustomerResource</td> |
| 105 |
<td>PUT</td> |
| 106 |
</tr> |
| 107 |
</table> |
| 108 |
|
| 109 |
<h2>Running the Example</h2> |
| 110 |
|
| 111 |
<p> |
| 112 |
You will need to manually deploy the example web application archive to your web container. |
| 113 |
The simplest way to try this out from the command line could be: |
| 114 |
<pre> |
| 115 |
cd webapp && mvn clean package embedded-glassfish:run |
| 116 |
</pre> |
| 117 |
Then you may want to open <a href="http://localhost:8080/moxy/customers">http://localhost:8080/moxy/customers</a> in your favourite web browser. |
| 118 |
If you will, go ahead and check the source code to see, that the Java beans in the beans module do not contain any JAXB annotations, and the web application is still able to generate XML out of it. |
| 119 |
</p> |
| 120 |
</body></html> |