| 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>Entity Provider Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>Entity Provider Example</h1> |
| 48 |
<p>This example demonstrates how to add support for custom |
| 49 |
Java types as resource class method parameters.</p> |
| 50 |
|
| 51 |
<h2>Contents</h2> |
| 52 |
<p>The example consists of seven Java classes:</p> |
| 53 |
<dl> |
| 54 |
<dt><code>com.sun.jersey.samples.entityprovider.Main</code></dt> |
| 55 |
<dd>Contains the main function which starts and stops the HTTP server.</dd> |
| 56 |
<dt><code>com.sun.jersey.samples.entityprovider.resources.MapResource</code></dt> |
| 57 |
<dd>A resource class that provides read and write access to a |
| 58 |
<code>Hashtable</code>.</dd> |
| 59 |
<dt><code>com.sun.jersey.samples.entityprovider.resources.PropertiesResource</code></dt> |
| 60 |
<dd>A resource class that provides read access to the system properties as plain text.</dd> |
| 61 |
<dt><code>com.sun.jersey.samples.entityprovider.resources.NameValuePair</code></dt> |
| 62 |
<dd>A utility class used to update <code>MapResource</code>. The input HTML form |
| 63 |
data is converted into an instance of this class by <code>FormReader</code>.</dd> |
| 64 |
<dt><code>com.sun.jersey.samples.entityprovider.FormReader</code></dt> |
| 65 |
<dd>A message body reader that can read HTML form data and produce an |
| 66 |
instance of <code>NameValuePair</code>. This class is used to instantiate the |
| 67 |
<code>NameValuePair</code> parameter of <code>MapResource.updateDataItem()</code>.</dd> |
| 68 |
<dt><code>com.sun.jersey.samples.entityprovider.FormWriter</code></dt> |
| 69 |
<dd>A message body writer that can write an HTML page that contains |
| 70 |
the contents of a <code>Hashtable</code> along with a HTML form |
| 71 |
for modifying the contents. This class is used to render the |
| 72 |
output of <code>MapResource.getData()</code> and <code>MapResource.updateDataItem()</code>.</dd> |
| 73 |
<dt><code>com.sun.jersey.samples.entityprovider.PropertiesProvider</code></dt> |
| 74 |
<dd>A message body writer that can write a plain text representation of |
| 75 |
a <code>Properties</code> instance. This class is used to render the |
| 76 |
output of <code>PropertiesResource.getSystemProperties()</code>.</dd> |
| 77 |
</dl> |
| 78 |
|
| 79 |
<p>Jersey uses the following files to find the message body readers and writers |
| 80 |
(<code>FormReader</code>, <code>FormWriter</code> and <code>PropertiesProvider</code>):</p> |
| 81 |
<dl> |
| 82 |
<dt><code>META-INF/services/javax.ws.rs.ext.MessageBodyReader</code></dt> |
| 83 |
<dd>Lists each class that implements <code>MessageBodyReader</code> |
| 84 |
(<code>FormReader</code>).</dd> |
| 85 |
<dt><code>META-INF/services/javax.ws.rs.ext.MessageBodyWriter</code></dt> |
| 86 |
<dd>Lists each class that implements <code>MessageBodyWriter</code> |
| 87 |
(<code>FormWriter</code> and <code>PropertiesProvider</code>).</dd> |
| 88 |
</dl> |
| 89 |
<p>Annotations on each message body reader and writer indicate the supported |
| 90 |
media types.</p> |
| 91 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 92 |
<table border="1"> |
| 93 |
<tr> |
| 94 |
<th>URI path</th> |
| 95 |
<th>Resource class</th> |
| 96 |
<th>HTTP methods</th> |
| 97 |
</tr> |
| 98 |
<tr> |
| 99 |
<td>/data</td> |
| 100 |
<td>MapResource</td> |
| 101 |
<td>GET, POST</td> |
| 102 |
</tr> |
| 103 |
<tr> |
| 104 |
<td>/properties</td> |
| 105 |
<td>PropertiesResource</td> |
| 106 |
<td>GET</td> |
| 107 |
</tr> |
| 108 |
</table> |
| 109 |
|
| 110 |
<h2>Running the Example</h2> |
| 111 |
<p>Run the example as follows:</p> |
| 112 |
<blockquote><pre> |
| 113 |
mvn compile exec:java</pre></blockquote> |
| 114 |
|
| 115 |
<p>From a web browser, visit:</p> |
| 116 |
<blockquote><code><a href="http://127.0.0.1:9998/properties">http://127.0.0.1:9998/properties</a></code></blockquote> |
| 117 |
<p>From a web browser, visit:</p> |
| 118 |
<blockquote><code><a href="http://127.0.0.1:9998/data">http://127.0.0.1:9998/data</a></code></blockquote> |
| 119 |
<p>Fill in the form and submit it. Note that the list below the form is |
| 120 |
updated each time the form is submitted.</p> |
| 121 |
|
| 122 |
<p>A <a href="http://wadl.java.net/#spec">WADL description</a> may be accessed at the URL:</p> |
| 123 |
<blockquote><code><a href="http://127.0.0.1:9998/application.wadl">http://127.0.0.1:9998/application.wadl</a></code></blockquote> |
| 124 |
|
| 125 |
</body></html> |