blob: 1afb6459c8a6898beff7b2849c0a453138ca9737 [file] [log] [blame]
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.7.4 at 2017-11-22
| Rendered using Apache Maven Fluido Skin 1.6
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="Date-Revision-yyyymmdd" content="20171122" />
<meta http-equiv="Content-Language" content="en" />
<title>Apache Axis2 &#x2013; JSON Support in Axis2</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-1.6.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script type="text/javascript" src="../js/apache-maven-fluido-1.6.min.js"></script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left"><a href="http://www.apache.org/" id="bannerLeft"><img src="http://www.apache.org/images/asf_logo_wide.png" alt="Apache Axis2"/></a></div>
<div class="pull-right"><a href=".././" id="bannerRight"><img src="../images/axis.jpg" /></a></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2017-11-22<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 1.7.7<span class="divider">|</span></li>
<li class=""><a href="http://www.apache.org" class="externalLink" title="Apache">Apache</a><span class="divider">/</span></li>
<li class=""><a href="../index.html" title="Axis2/Java">Axis2/Java</a><span class="divider">/</span></li>
<li class="active ">JSON Support in Axis2</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span2">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Axis2/Java</li>
<li><a href="../index.html" title="Home"><span class="none"></span>Home</a> </li>
<li><a href="../download.html" title="Downloads"><span class="none"></span>Downloads</a> </li>
<li><a href="javascript:void(0)" title="Release Notes"><span class="icon-chevron-down"></span>Release Notes</a>
<ul class="nav nav-list">
<li><a href="../release-notes/1.6.1.html" title="1.6.1"><span class="none"></span>1.6.1</a> </li>
<li><a href="../release-notes/1.6.2.html" title="1.6.2"><span class="none"></span>1.6.2</a> </li>
<li><a href="../release-notes/1.6.3.html" title="1.6.3"><span class="none"></span>1.6.3</a> </li>
<li><a href="../release-notes/1.6.4.html" title="1.6.4"><span class="none"></span>1.6.4</a> </li>
<li><a href="../release-notes/1.7.0.html" title="1.7.0"><span class="none"></span>1.7.0</a> </li>
<li><a href="../release-notes/1.7.1.html" title="1.7.1"><span class="none"></span>1.7.1</a> </li>
<li><a href="../release-notes/1.7.2.html" title="1.7.2"><span class="none"></span>1.7.2</a> </li>
<li><a href="../release-notes/1.7.3.html" title="1.7.3"><span class="none"></span>1.7.3</a> </li>
<li><a href="../release-notes/1.7.4.html" title="1.7.4"><span class="none"></span>1.7.4</a> </li>
<li><a href="../release-notes/1.7.5.html" title="1.7.5"><span class="none"></span>1.7.5</a> </li>
<li><a href="../release-notes/1.7.6.html" title="1.7.6"><span class="none"></span>1.7.6</a> </li>
<li><a href="../release-notes/1.7.7.html" title="1.7.7"><span class="none"></span>1.7.7</a> </li>
</ul>
</li>
<li><a href="../modules/index.html" title="Modules"><span class="none"></span>Modules</a> </li>
<li><a href="../tools/index.html" title="Tools"><span class="none"></span>Tools</a> </li>
<li class="nav-header">Documentation</li>
<li><a href="../docs/toc.html" title="Table of Contents"><span class="none"></span>Table of Contents</a> </li>
<li><a href="../docs/installationguide.html" title="Installation Guide"><span class="none"></span>Installation Guide</a> </li>
<li><a href="../docs/quickstartguide.html" title="QuickStart Guide"><span class="none"></span>QuickStart Guide</a> </li>
<li><a href="../docs/userguide.html" title="User Guide"><span class="none"></span>User Guide</a> </li>
<li><a href="../docs/jaxws-guide.html" title="JAXWS Guide"><span class="none"></span>JAXWS Guide</a> </li>
<li><a href="../docs/pojoguide.html" title="POJO Guide"><span class="none"></span>POJO Guide</a> </li>
<li><a href="../docs/spring.html" title="Spring Guide"><span class="none"></span>Spring Guide</a> </li>
<li><a href="../docs/webadminguide.html" title="Web Administrator's Guide"><span class="none"></span>Web Administrator's Guide</a> </li>
<li><a href="../docs/migration.html" title="Migration Guide (from Axis1)"><span class="none"></span>Migration Guide (from Axis1)</a> </li>
<li class="nav-header">Resources</li>
<li><a href="../faq.html" title="FAQ"><span class="none"></span>FAQ</a> </li>
<li><a href="../articles.html" title="Articles"><span class="none"></span>Articles</a> </li>
<li><a href="http://wiki.apache.org/ws/FrontPage/Axis2/" class="externalLink" title="Wiki"><span class="none"></span>Wiki</a> </li>
<li><a href="../refLib.html" title="Reference Library"><span class="none"></span>Reference Library</a> </li>
<li><a href="../apidocs/index.html" title="Online Java Docs"><span class="none"></span>Online Java Docs</a> </li>
<li class="nav-header">Get Involved</li>
<li><a href="../overview.html" title="Overview"><span class="none"></span>Overview</a> </li>
<li><a href="../svn.html" title="Checkout the Source"><span class="none"></span>Checkout the Source</a> </li>
<li><a href="../mail-lists.html" title="Mailing Lists"><span class="none"></span>Mailing Lists</a> </li>
<li><a href="../release-process.html" title="Release Process"><span class="none"></span>Release Process</a> </li>
<li><a href="../guidelines.html" title="Developer Guidelines"><span class="none"></span>Developer Guidelines</a> </li>
<li><a href="../siteHowTo.html" title="Build the Site"><span class="none"></span>Build the Site</a> </li>
<li class="nav-header">Project Information</li>
<li><a href="../team-list.html" title="Project Team"><span class="none"></span>Project Team</a> </li>
<li><a href="../issue-tracking.html" title="Issue Tracking"><span class="none"></span>Issue Tracking</a> </li>
<li><a href="http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/" class="externalLink" title="Source Code"><span class="none"></span>Source Code</a> </li>
<li><a href="../thanks.html" title="Acknowledgements"><span class="none"></span>Acknowledgements</a> </li>
<li class="nav-header">Apache</li>
<li><a href="http://www.apache.org/licenses/LICENSE-2.0.html" class="externalLink" title="License"><span class="none"></span>License</a> </li>
<li><a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink" title="Sponsorship"><span class="none"></span>Sponsorship</a> </li>
<li><a href="http://www.apache.org/foundation/thanks.html" class="externalLink" title="Thanks"><span class="none"></span>Thanks</a> </li>
<li><a href="http://www.apache.org/security/" class="externalLink" title="Security"><span class="none"></span>Security</a> </li>
</ul>
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a>
</div>
</div>
</div>
<div id="bodyColumn" class="span10" >
<h1>JSON Support in Axis2</h1>
<div class="section">
<h2><a name="Introduction"></a>Introduction</h2>
<p>This document explains the JSON support implementation in Axis2.
It includes an introduction to JSON, an outline as to why JSON
support is useful to Axis2 and how it should be used. This document
also provides details on test cases and samples.</p>
</div>
<div class="section">
<h2><a name="What_is_JSON"></a>What is JSON?</h2>
<p><a class="externalLink" href="http://www.json.org/">JSON</a> (Java Script Object
Notation) is another data exchangeable format like XML, but more
lightweight and easily readable. It is based on a subset of the
JavaScript language. Therefore, JavaScript can understand JSON, and
it can make JavaScript objects by using JSON strings. JSON is based
on key-value pairs and it uses colons to separate keys and values.
JSON doesn't use end tags, and it uses braces (curly brackets) to
enclose JSON Objects.</p>
<p><font size="3">e.g. <font size="2">&lt;root&gt;&lt;test&gt;json
object&lt;/test&gt;&lt;/root&gt; ==
{{json object}}</font></font></p>
<p>When it comes to converting XML to JSON and vice versa, there
are two major conventions, one named &quot;<a class="externalLink" href="http://www.sklar.com/badgerfish/">Badgerfish</a>&quot; and the other,
Mapped. The main difference
between these two conventions exists in the way they map XML
namespaces into JSON.</p>
<p><font size="3">e.g. <font size="2">&lt;xsl:root
xmlns:xsl=&quot;http://foo.com&quot;&gt;&lt;data&gt;my json
string&lt;/data&gt;&lt;/xsl:root&gt;</font></font></p>
<p>This XML string can be converted into JSON as follows.</p>
<p><b>Using Badgerfish</b></p>
<p><font size="2">{&quot;xsl:root&quot;:{&quot;@xmlns&quot;:{&quot;xsl&quot;:&quot;http://foo.com&quot;},&quot;data&quot;:{&quot;$&quot;:&quot;my
json string&quot;}}}</font></p>
<p><b>Using Mapped</b></p>
<p>If we use the namespace mapping as http://foo.com -&gt; foo</p>
<p><font size="2">{&quot;foo.root&quot;:{&quot;data&quot;:&quot;my json string&quot;}}</font></p>
</div>
<div class="section">
<h2><a name="Why_JSON_Support_for_Axis2"></a>Why JSON Support for Axis2?</h2>
<p><a href="../index.html">Apache Axis2</a> is a Web
services stack that delivers incoming messages into target
applications. In most cases, these messages are SOAP messages. In
addition, it is also possible to send REST messages through Axis2.
Both types of messages use XML as their data exchangeable format.
So if we can use XML as a format, why use JSON as another
format?</p>
<p>There are many advantages of implementing JSON support in Axis2.
Mainly, it helps the JavaScript users (services and clients written
in JavaScript) to deal with Axis2. When the service or the client
is in JavaScript, it can use the JSON string and directly build
JavaScript objects to retrieve information, without having to build
the object model (OMElement in Axis2). Also, JavaScript services
can return the response through Axis2, just as a JSON string can be
shipped in a JSONDataSource.</p>
<p>Other than for that, there are some extra advantages of using
JSON in comparison to XML. Although the conversation
XML or JSON? is still a hot topic,
many people accept the fact that JSON can be passed and built more
easily by machines than XML.</p>
</div>
<div class="section">
<h2><a name="How_to_use_JSON_in_Axis2"></a>How to use JSON in Axis2</h2>
<p>At the moment JSON doesn't have a standard and unique content
type. <tt>application/json</tt> (this is
the content type which is approved in the <a class="externalLink" href="http://www.ietf.org/rfc/rfc4627.txt">JSON RFC</a>),
<tt>text/javascript</tt> and
<tt>text/json</tt> are some of the commonly
used content types for JSON. Fortunately, in Axis2, the user
has the freedom of specifying the content type to use.</p>
<div class="section">
<h3><a name="Configuring_axis2.xml"></a>Configuring axis2.xml</h3>
<p>First of all, you need to map the appropriate message formatters and builders to the
content type you are using in the <tt>axis2.xml</tt> file. This applies both the to
client side and the server side.</p>
<p>E.g., if you are using the
Mapped convention with the content
type <tt>application/json</tt>, add the following declaration:</p>
<div>
<pre>
&lt;messageFormatters&gt;
&lt;messageFormatter contentType=&quot;application/json&quot;
class=&quot;org.apache.axis2.json.JSONMessageFormatter&quot;/&gt;
&lt;!-- more message formatters --&gt;
&lt;/messageFormatters&gt;
&lt;messageBuilders&gt;
&lt;messageBuilder contentType=&quot;application/json&quot;
class=&quot;org.apache.axis2.json.JSONOMBuilder&quot;/&gt;
&lt;!-- more message builders --&gt;
&lt;/messageBuilders&gt;
</pre></div>
<p>If you are using the
Badgerfish convention with the
content type <tt>text/javascript</tt>, add:</p>
<div>
<pre>
&lt;messageFormatters&gt;
&lt;messageFormatter contentType=&quot;text/javascript&quot;
class=&quot;org.apache.axis2.json.JSONBadgerfishMessageFormatter&quot;&gt;
&lt;!-- more message formatters --&gt;
&lt;/messageFormatters&gt;
&lt;messageBuilders&gt;
&lt;messageBuilder contentType=&quot;text/javascript&quot;
class=&quot;org.apache.axis2.json.JSONBadgerfishOMBuilder&quot;/&gt;
&lt;!-- more message builders --&gt;
&lt;/messageBuilders&gt;
</pre></div>
</div>
<div class="section">
<h3><a name="Client-side_configuration"></a>Client-side configuration</h3>
<p>On the client side, make the ConfigurationContext by reading the
axis2.xml in which the correct mappings are given.</p>
<p>e.g.</p>
<div>
<pre>
File configFile = new File(&quot;test-resources/axis2.xml&quot;);
configurationContext = ConfigurationContextFactory
.createConfigurationContextFromFileSystem(null, configFile.getAbsolutePath());
..........
ServiceClient sender = new ServiceClient(configurationContext, null);
</pre></div>
<p>Set the <i>MESSAGE_TYPE</i> option with exactly the same content
type you used in the axis2.xml.</p>
<p>e.g. If you use the content type
application/json,</p>
<div>
<pre>
Options options = new Options();
options.setProperty(Constants.Configuration.MESSAGE_TYPE, &quot;application/json&quot;);
//more options
//...................
ServiceClient sender = new ServiceClient(configurationContext, null);
sender.setOptions(options);
</pre></div>
<p>If you are sending a request to a remote service, you have to
know the exact JSON content type that is used by that service, and
you have to use that content type in your client as well.</p>
<p>HTTP POST is used as the default method to send JSON messages
through Axis2, if the HTTP method is not explicitly set by the
user. But if you want to send JSON in HTTP GET method as a
parameter, you can do that by just setting an option on the client
side.</p>
<p>e.g.</p>
<tt>options.setProperty(Constants.Configuration.HTTP_METHOD,
Constants.Configuration.HTTP_METHOD_GET);</tt>
<p>Here, the Axis2 receiving side (JSONOMBuilder) builds the
OMElement by reading the JSON string which is sent as a parameter.
The request can be made even through the browser.</p>
<p>e.g. Sample JSON request through HTTP GET. The JSON message is
encoded and sent.</p>
<tt>GET
/axis2/services/EchoXMLService/echoOM?query=%7B%22echoOM%22:%7B%22data%22:%5B%22my%20json%20string%22,%22my%20second%20json%20string%22%5D%7D%7D
HTTP/1.1</tt>
</div>
<div class="section">
<h3><a name="Server-side_configuration"></a>Server-side configuration</h3>
<p>Since Badgerfish defines a 1-to-1 transformation between JSON and XML, no additional configuration
is required on the server side if that convention is used. Any service deployed into Axis2 will work
out of the box.</p>
<p>On the other hand, if the Mapped JSON convention is used, then Axis2 needs to know the mappings
between XML namespaces and JSON &quot;namespaces&quot; in order to translate messages from JSON
into XML representations and vice-versa. To use the Mapped convention with a service deployed into Axis2,
add a <tt>xmlToJsonNamespaceMap</tt> property with these mappings to the <tt>services.xml</tt> file for that service, as
shown in the following example:</p>
<div>
<pre>
&lt;service name=&quot;...&quot;&gt;
...
&lt;parameter name=&quot;xmlToJsonNamespaceMap&quot;&gt;
&lt;mappings&gt;
&lt;mapping xmlNamespace=&quot;http://example.org/foo&quot; jsonNamespace=&quot;&quot;/&gt;
&lt;mapping xmlNamespace=&quot;http://example.org/bar&quot; jsonNamespace=&quot;bar&quot;/&gt;
&lt;/mappings&gt;
&lt;/parameter&gt;
...
&lt;/service&gt;
</pre></div>
</div>
</div>
<div class="section">
<h2><a name="How_the_JSON_implementation_works_-_Architecture"></a>How the JSON implementation works - Architecture</h2>
<div class="section">
<h3><a name="Introduction"></a>Introduction</h3>
<p>The Axis2 architecture is based on the assumption that any message flowing through
the Axis2 runtime is representable as a SOAP infoset, i.e. as XML wrapped in a SOAP
envelope. Conceptually, the two message builders <tt>JSONOMBuilder</tt> and
<tt>JSONBadgerfishOMBuilder</tt> convert incoming messages from JSON to XML and
the two message formatters <tt>JSONMessageFormatter</tt> and <tt>JSONBadgerfishMessageFormatter</tt>
convert outgoing messages from XML to JSON. Axis2 doesn't implement its own JSON parser and serializer, and
instead relies on <a class="externalLink" href="http://jettison.codehaus.org/">Jettison</a> to do the JSON&lt;-&gt;XML conversions.</p>
<p>On the server side the XML for an incoming
message is typically converted to Java objects by a databinding (such as ADB or JAX-WS)
before the invocation of the service implementation. In the same way, the Java object returned by the
service implementation is converted to XML. In the case we are interested in, that XML is then converted
by the message formatters to JSON. The usage of an intermediate XML representation is the reason why
JSON can be enabled on any service deployed in Axis2.</p>
<p>It is important to note that the explanation given in the previous two paragraphs is only valid from
a conceptual point of view. The actual processing model is more complicated. In the next two sections
we will explain in detail how Axis2 processes incoming and outgoind JSON messages.</p>
</div>
<div class="section">
<h3><a name="Processing_of_incoming_JSON_messages"></a>Processing of incoming JSON messages</h3>
<p>Axis2 relies on <a class="externalLink" href="http://ws.apache.org/axiom/">Apache Axiom</a> as its XML object model. Although
Axiom has a DOM like API, it also has several advanced features that enable Axis2 to avoid
building a complete object model representation of the XML message. This is important for performance
reasons and distinguishes Axis2 from previous generation SOAP stacks. To leverage these features, the
JSON message builders create a SOAP envelope the body of which contains a single <tt>OMSourcedElement</tt>.</p>
<p>An <tt>OMSourcedElement</tt> is a special kind of <tt>OMElement</tt> that wraps an arbitrary
Java object that can be converted to XML in a well defined way. More precisely, the Java object as well as the logic
to convert the object to XML are encapsulated in an <tt>OMDataSource</tt> instance and it is that
<tt>OMDataSource</tt> instance that is used to create the <tt>OMSourcedElement</tt>.
For JSON, the <tt>OMDataSource</tt> implementation is <tt>JSONDataSource</tt> or <tt>JSONBadgerfishDataSource</tt>,
depending on the convention being used. The base class (<tt>AbstractJSONDataSource</tt>) of these two classes
actually contains the code that invokes Jettison to perform the JSON to XML conversion.</p>
<p>An <tt>OMSourcedElement</tt> still behaves like a normal <tt>OMElement</tt>. In particular, if the
element is accessed using DOM like methods, then Axiom will convert the data encapsulated by
the <tt>OMDataSource</tt> on the fly to an object model representation. This process is called <i>expansion</i> of the
<tt>OMSourcedElement</tt>. However, the <tt>OMDataSource</tt> API is designed such that the conversion to
XML is always done using a streaming API: either the <tt>OMDataSource</tt> produces an <tt>XMLStreamReader</tt>
instance from which the XML representation can be read (this is the case for JSON and the <tt>XMLStreamReader</tt> implementation
is actually provided by Jettison) or it serializes the XML representation to an <tt>XMLStreamWriter</tt>.
Because of this, expansion of the <tt>OMSourcedElement</tt> is often not necessary, so that the overhead of
creating an object model representation can usually be avoided. E.g. a databinding will typically consume the message by requesting an
<tt>XMLStreamReader</tt> for the element in the SOAP body, and this doesn't require expansion of the
<tt>OMSourcedElement</tt>. In this case, the databinding pulls the XML data almost directly from the
underlying Jettison <tt>XMLStreamReader</tt> and no additional Axiom objects are created.</p>
<p>Actually here again, things are slightly more complicated because in order to dispatch to the right
operation, Axis2 needs to determine the name of the element in the body. Since the name is not known
in advance, that operation requires expansion of the <tt>OMSourcedElement</tt>. However, at this point
none of the children of the <tt>OMSourcedElement</tt> will be built. Fortunately the databindings
generally request the <tt>XMLStreamReader</tt> with caching turned off, so that the child nodes will never be
built. Therefore the conclusion of the previous paragraph remains valid: processing the message with a databinding
will not create a complete object model representation of the XML.</p>
<p>Usage of an <tt>OMSourcedElement</tt> also solves another architectural challenge posed by
the Mapped JSON convention: the JSON payload can only be converted to XML if the namespace mappings
are known. Since they are defined per service, they are only known after the incoming message has been
dispatched and the target service has been identified. This typically occurs
in <tt>RequestURIBasedDispatcher</tt>, which is executed after
the message builder. This means that <tt>JSONOMBuilder</tt> cannot actually perform the conversion.
Usage of an <tt>OMSourcedElement</tt> avoids this issue because the conversion is done lazily when
the <tt>OMSourcedElement</tt> is first accessed, and this occurs after <tt>RequestURIBasedDispatcher</tt>
has been executed.</p>
<p>Another advantage of using <tt>OMSourcedElement</tt> is that a JSON aware service could directly process
the JSON payload without going through the JSON to XML conversion. That is possible because the <tt>OMDataSource</tt>
simply keeps a reference to the JSON payload and this reference is accessible to JSON aware code.</p>
</div>
<div class="section">
<h3><a name="Processing_of_outgoing_messages"></a>Processing of outgoing messages</h3>
<p>For outgoing messages, the two JSON message formatters <tt>JSONMessageFormatter</tt> and
<tt>JSONBadgerfishMessageFormatter</tt> use Jettision to create an appropriate <tt>XMLStreamWriter</tt>
and then request Axiom to serialize the body element to that <tt>XMLStreamWriter</tt>. If a databinding
is used, then the body element will typically be an <tt>OMSourcedElement</tt> with an <tt>OMDataSource</tt>
implementation specific to that databinding. <tt>OMSourcedElement</tt> will delegate the serialization
request to the appropriate method defined by <tt>OMDataSource</tt>. This means that the databinding code
directly writes to the <tt>XMLStreamWriter</tt> instance provided by Jettision, without building an
intermediate XML object model.</p>
<p>Before doing this, the JSON message formatters actually check if the element is an <tt>OMSourcedElement</tt>
backed by a corresponding JSON <tt>OMDataSource</tt> implementation. If that is the case, then they will
extract the JSON payload and directly write it to the output stream. This allows JSON aware services to
bypass the XML to JSON conversion entirely.</p>
</div>
</div>
<div class="section">
<h2><a name="Tests_and_Samples"></a>Tests and Samples</h2>
<div class="section">
<h3><a name="Integration_Test"></a>Integration Test</h3>
<p>The JSON integration test is available under
test in the
json module of Axis2. It uses the
SimpleHTTPServer to deploy the service. A simple echo service is
used to return the incoming OMSourcedElement object, which
contains the JSONDataSource. There are two test cases for two
different conventions and another one test case to send the request
in GET.</p>
</div>
<div class="section">
<h3><a name="Yahoo-JSON_Sample"></a>Yahoo-JSON Sample</h3>
<p>This sample is available in the
samples module of Axis2. It is a
client which calls the Yahoo search API using the GET method, with
the parameter output=json. The
Yahoo search service sends the response as a
formatted JSON string with
the content type text/javascript.
This content type is mapped with the JSONOMBuilder in the
axis2.xml. All the results are shown in a GUI. To run the sample,
execute the ant script.</p>
<p>These two applications provide good examples of using JSON
within Axis2. By reviewing these samples, you will be able to
better understand Axis2's JSON support implementation.</p>
</div>
<div class="section">
<h3><a name="Enabling_mapped_JSON_on_the_ADB_quickstart_sample"></a>Enabling mapped JSON on the ADB quickstart sample</h3>
<p>To illustrate how JSON can be enabled on an existing service deployed in Axis2,
we will use the ADB stock quote service sample from the
<a href="quickstartguide.html#adb">Quick Start Guide</a>. The code for this sample
can be found in the <tt>samples/quickstartadb</tt> folder in the binary distribution.</p>
<p>Only a few steps are necessary to enable JSON (using the Mapped convention) on
that service:</p>
<ol style="list-style-type: decimal">
<li>
<p>Configure the JSON message builders and formatters in <tt>conf/axis2.xml</tt>.
Add the following element to the <tt>messageFormatters</tt>:</p>
<div>
<pre>
&lt;messageFormatter contentType=&quot;application/json&quot;
class=&quot;org.apache.axis2.json.JSONMessageFormatter&quot;/&gt;
</pre></div>
<p>Also add the following element to the <tt>messageBuilders:</tt></p>
<div>
<pre>
&lt;messageBuilder contentType=&quot;application/json&quot;
class=&quot;org.apache.axis2.json.JSONOMBuilder&quot;/&gt;
</pre></div>
</li>
<li>
<p>Edit the <tt>services.xml</tt> for the stock quote service and add the following
configuration:</p>
<div>
<pre>
&lt;parameter name=&quot;xmlToJsonNamespaceMap&quot;&gt;
&lt;mappings&gt;
&lt;mapping xmlNamespace=&quot;http://quickstart.samples/xsd&quot; jsonNamespace=&quot;&quot;/&gt;
&lt;/mappings&gt;
&lt;/parameter&gt;
</pre></div>
<p>The <tt>services.xml</tt> file can be found under
<tt>samples/quickstartadb/resources/META-INF</tt>.</p>
</li>
<li>
<p>Build and deploy the service by executing the ant script in
<tt>samples/quickstartadb</tt> and then start the Axis2 server using
<tt>bin/axis2server.sh</tt> or <tt>bin/axis2server.bat</tt>.</p>
</li>
</ol>
<p>That's it; the stock quote service can now be invoked using JSON. This can be tested
using the well known <a class="externalLink" href="http://curl.haxx.se/">curl</a> tool:</p>
<div>
<pre>curl -H 'Content-Type: application/json' -d '{&quot;getPrice&quot;:{&quot;symbol&quot;:&quot;IBM&quot;}}' http://localhost:8080/axis2/services/StockQuoteService</pre></div>
<p>This will give the following result:</p>
<div>
<pre>{&quot;getPriceResponse&quot;:{&quot;return&quot;:42}}</pre></div>
</div>
</div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>Copyright &copy;2004&#x2013;2017
<a href="https://www.apache.org/">The Apache Software Foundation</a>.
All rights reserved.</p>
</div>
</div>
</footer>
</body>
</html>