json-jackson1
Directory actions
More options
Directory actions
More options
json-jackson1
Folders and files
| Name | Name | 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-2014 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>Jackson (1.x) JAX-RS JSON Provider Example</title>
<style type="text/css"><!--
.figure { text-align:center;
margin: 20px}
.cliSample { background-color:lightgray }
--></style>
</head>
<body>
<h1>Jackson (1.x) JAX-RS JSON Provider Example</h1>
<p>This example demonstrates how to produce/consume JSON representations from Java objects. This applies not only to JAXB beans, as shown in the <tt>json-from-jaxb</tt> example
but also to ordinary, un-annotated, POJOs.
<p>This example hosts three simple read-only resources: One provides an example of using a Jackson JSON provider (registered by the feature <code>Jackson1Feature</code> in the <code>MyApplication</code> class)
instead of using JAXB (Object->JAXB->JSON) which has some limitations (e.g. empty arrays in JAXB beans). For this resource the JSON representation is produced by the Jackson JAX-RS provider, while the XML representation is generated by JAXB as usual.
The second web resource is based on a simple un-annotated POJO and provides only JSON-based representations: JSON and JSON with padding (JSONP).
The third resource depicts how Jackson and JAXB annotations could be mixed together within a single POJO.
<h2>Contents</h2>
<p>The "empty array" web resource is implemented by the <code>org.glassfish.jersey.examples.jackson1.EmptyArrayResource</code> class.
The "non JAXB" web resource is implemented by the <code>org.glassfish.jersey.examples.jackson1.NonJaxbBeanResource</code> class.
Both resources use the default Jackson mapper configuration to serialize JSON data out and depicts the corner cases, where
the Jersey internal, StAX based, JSON processing can not be utilized.
<p>The <code>org.glassfish.jersey.examples.jackson1.CombinedAnnotationResource</code> class is used to show how JAXB and Jackson
annotations could be combined together in one Java bean for JSON serialization.
See the <code>org.glassfish.jersey.examples.jackson1.MyObjectMapperProvider</code>
class where Jackson specific options are used to set up the desired JSON serialization configuration.
<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>/emptyArrayResource</td>
<td>EmptyArrayResource</td>
<td>GET</td>
</tr>
<tr>
<td>/nonJaxbResource</td>
<td>NonJaxbBeanResource</td>
<td>GET</td>
</tr>
<tr>
<td>/combinedAnnotations</td>
<td>CombinedAnnotationResource</td>
<td>GET</td>
</tr>
</table>
<p>To use Jackson specific configuration options, one can implement
a <code>ContextResolver<ObjectMapper></code> provider.
For an example of such an implementation, see the <code>MyObjectMapperProvider</code> class.
<h2>Running the Example</h2>
<p>Run the example as follows:</p>
<blockquote><pre>
mvn clean compile exec:java</pre></blockquote>
<p>This deploys the example using
<a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fdocker-java%2Fjersey%2Ftree%2Fmaster%2Fexamples%2F%3Ca+href%3D"http://grizzly.java.net/">Grizzly</a" rel="nofollow">http://grizzly.java.net/">Grizzly</a>
</p>
<p>A <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fdocker-java%2Fjersey%2Ftree%2Fmaster%2Fexamples%2F%3Ca+href%3D"http://wadl.java.net/#spec">WADL" rel="nofollow">http://wadl.java.net/#spec">WADL description</a> may be accessed at the URL:</p>
<blockquote><code><a href="proxy.php?url=http%3A%2F%2Flocalhost%3A8080%2Fjackson1%2Fapplication.wadl">http://localhost:8080/jackson1/application.wadl</a></code></blockquote>
<p>The three resources are available at</p>
<blockquote><code><a href="proxy.php?url=http%3A%2F%2Flocalhost%3A8080%2Fjackson1%2FemptyArrayResource">http://localhost:8080/jackson1/emptyArrayResource</a></code></blockquote>
<blockquote><code><a href="proxy.php?url=http%3A%2F%2Flocalhost%3A8080%2Fjackson1%2FnonJaxbResource">http://localhost:8080/jackson1/nonJaxbResource</a></code></blockquote>
<blockquote><code><a href="proxy.php?url=http%3A%2F%2Flocalhost%3A8080%2Fjackson1%2FcombinedAnnotations">http://localhost:8080/jackson1/combinedAnnotations</a></code></blockquote>
<p>To easily obtain the different output types available, <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fdocker-java%2Fjersey%2Ftree%2Fmaster%2Fexamples%2F%3Ca+href%3D"http://curl.haxx.se/">cURL</a" rel="nofollow">http://curl.haxx.se/">cURL</a> can be used as follows:</p>
<p>Obtain the JSON output of EmptyArrayResource (use <code>-HAccept:application/xml</code> for XML output):</p>
<blockquote><pre>curl -HAccept:application/json http://localhost:8080/jackson1/emptyArrayResource</pre></blockquote>
<p>Obtain the JSON output of NonJaxbBeanResource (use <code>-HAccept:application/javascript</code> for JSONP output):</p>
<blockquote><pre>curl -HAccept:application/json http://localhost:8080/jackson1/nonJaxbResource</pre></blockquote>
<p>Obtain the JSON output of CombinedAnnotationResource:</p>
<blockquote><pre>curl -HAccept:application/json http://localhost:8080/jackson1/combinedAnnotations</pre></blockquote>
</body>
</html>