| <!DOCTYPE html> |
| <!-- |
| | Generated by Apache Maven Doxia Site Renderer 1.11.1 at 2022-06-07 |
| | 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="20220607" /> |
| <meta http-equiv="Content-Language" content="en" /> |
| <title>Apache Axis2 – 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: 2022-06-07<span class="divider">|</span> |
| </li> |
| <li id="projectVersion">Version: 1.8.1<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> |
| <li><a href="../release-notes/1.7.8.html" title="1.7.8"><span class="none"></span>1.7.8</a> </li> |
| <li><a href="../release-notes/1.7.9.html" title="1.7.9"><span class="none"></span>1.7.9</a> </li> |
| <li><a href="../release-notes/1.8.0.html" title="1.8.0"><span class="none"></span>1.8.0</a> </li> |
| <li><a href="../release-notes/1.8.1.html" title="1.8.1"><span class="none"></span>1.8.1</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="../git.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> |
| <section> |
| <h2><a name="Introduction"></a>Introduction</h2> |
| |
| <p>Update: This documentation represents early forms of JSON |
| conventions, Badgerfish and Mapped. GSON support was added a few |
| years later. Moshi support is now included as an alternative to |
| GSON. For users of JSON seeking modern features, see the <a href="json_support_gson.html">JSON Support Guide.</a>. For users of |
| JSON and Spring Boot, see the sample application in the <a href="json-springboot-userguide.html">JSON and Spring Boot User's Guide.</a> |
| </p> |
| |
| <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> |
| </section> |
| <section> |
| <h2><a name="What_is_JSON.3F"></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"><root><test>json |
| object</test></root> == |
| {{json object}}</font></font></p> |
| |
| <p>When it comes to converting XML to JSON and vice versa, there |
| are two major conventions, one named "<a class="externalLink" href="http://www.sklar.com/badgerfish/">Badgerfish</a>" 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"><xsl:root |
| xmlns:xsl="http://foo.com"><data>my json |
| string</data></xsl:root></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">{"xsl:root":{"@xmlns":{"xsl":"http://foo.com"},"data":{"$":"my |
| json string"}}}</font></p> |
| |
| <p><b>Using Mapped</b></p> |
| |
| <p>If we use the namespace mapping as http://foo.com -> foo</p> |
| |
| <p><font size="2">{"foo.root":{"data":"my json string"}}</font></p> |
| </section> |
| <section> |
| <h2><a name="Why_JSON_Support_for_Axis2.3F"></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> |
| </section> |
| <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. <code>application/json</code> (this is |
| the content type which is approved in the <a class="externalLink" href="http://www.ietf.org/rfc/rfc4627.txt">JSON RFC</a>), |
| <code>text/javascript</code> and |
| <code>text/json</code> 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> |
| <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 <code>axis2.xml</code> 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 <code>application/json</code>, add the following declaration:</p> |
| |
| <div> |
| <pre> |
| <messageFormatters> |
| <messageFormatter contentType="application/json" |
| class="org.apache.axis2.json.JSONMessageFormatter"/> |
| <!-- more message formatters --> |
| </messageFormatters> |
| |
| <messageBuilders> |
| <messageBuilder contentType="application/json" |
| class="org.apache.axis2.json.JSONOMBuilder"/> |
| <!-- more message builders --> |
| </messageBuilders> |
| </pre></div> |
| |
| <p>If you are using the |
| Badgerfish convention with the |
| content type <code>text/javascript</code>, add:</p> |
| |
| <div> |
| <pre> |
| <messageFormatters> |
| <messageFormatter contentType="text/javascript" |
| class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"> |
| <!-- more message formatters --> |
| </messageFormatters> |
| |
| <messageBuilders> |
| <messageBuilder contentType="text/javascript" |
| class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/> |
| <!-- more message builders --> |
| </messageBuilders> |
| </pre></div> |
| </section> |
| <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("test-resources/axis2.xml"); |
| 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, "application/json"); |
| //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> |
| <code>options.setProperty(Constants.Configuration.HTTP_METHOD, |
| Constants.Configuration.HTTP_METHOD_GET);</code> |
| |
| <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> |
| <code>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</code> |
| </section> |
| <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 "namespaces" 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 <code>xmlToJsonNamespaceMap</code> property with these mappings to the <code>services.xml</code> file for that service, as |
| shown in the following example:</p> |
| |
| <div> |
| <pre> |
| <service name="..."> |
| ... |
| <parameter name="xmlToJsonNamespaceMap"> |
| <mappings> |
| <mapping xmlNamespace="http://example.org/foo" jsonNamespace=""/> |
| <mapping xmlNamespace="http://example.org/bar" jsonNamespace="bar"/> |
| </mappings> |
| </parameter> |
| ... |
| </service> |
| </pre></div> |
| </section> |
| </section> |
| <section> |
| <h2><a name="How_the_JSON_implementation_works_-_Architecture"></a>How the JSON implementation works - Architecture</h2> |
| <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 <code>JSONOMBuilder</code> and |
| <code>JSONBadgerfishOMBuilder</code> convert incoming messages from JSON to XML and |
| the two message formatters <code>JSONMessageFormatter</code> and <code>JSONBadgerfishMessageFormatter</code> |
| 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<->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> |
| </section> |
| <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 <code>OMSourcedElement</code>.</p> |
| |
| <p>An <code>OMSourcedElement</code> is a special kind of <code>OMElement</code> 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 <code>OMDataSource</code> instance and it is that |
| <code>OMDataSource</code> instance that is used to create the <code>OMSourcedElement</code>. |
| For JSON, the <code>OMDataSource</code> implementation is <code>JSONDataSource</code> or <code>JSONBadgerfishDataSource</code>, |
| depending on the convention being used. The base class (<code>AbstractJSONDataSource</code>) of these two classes |
| actually contains the code that invokes Jettison to perform the JSON to XML conversion.</p> |
| |
| <p>An <code>OMSourcedElement</code> still behaves like a normal <code>OMElement</code>. In particular, if the |
| element is accessed using DOM like methods, then Axiom will convert the data encapsulated by |
| the <code>OMDataSource</code> on the fly to an object model representation. This process is called <i>expansion</i> of the |
| <code>OMSourcedElement</code>. However, the <code>OMDataSource</code> API is designed such that the conversion to |
| XML is always done using a streaming API: either the <code>OMDataSource</code> produces an <code>XMLStreamReader</code> |
| instance from which the XML representation can be read (this is the case for JSON and the <code>XMLStreamReader</code> implementation |
| is actually provided by Jettison) or it serializes the XML representation to an <code>XMLStreamWriter</code>. |
| Because of this, expansion of the <code>OMSourcedElement</code> 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 |
| <code>XMLStreamReader</code> for the element in the SOAP body, and this doesn't require expansion of the |
| <code>OMSourcedElement</code>. In this case, the databinding pulls the XML data almost directly from the |
| underlying Jettison <code>XMLStreamReader</code> 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 <code>OMSourcedElement</code>. However, at this point |
| none of the children of the <code>OMSourcedElement</code> will be built. Fortunately the databindings |
| generally request the <code>XMLStreamReader</code> 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 <code>OMSourcedElement</code> 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 <code>RequestURIBasedDispatcher</code>, which is executed after |
| the message builder. This means that <code>JSONOMBuilder</code> cannot actually perform the conversion. |
| Usage of an <code>OMSourcedElement</code> avoids this issue because the conversion is done lazily when |
| the <code>OMSourcedElement</code> is first accessed, and this occurs after <code>RequestURIBasedDispatcher</code> |
| has been executed.</p> |
| |
| <p>Another advantage of using <code>OMSourcedElement</code> 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 <code>OMDataSource</code> |
| simply keeps a reference to the JSON payload and this reference is accessible to JSON aware code.</p> |
| </section> |
| <section> |
| <h3><a name="Processing_of_outgoing_messages"></a>Processing of outgoing messages</h3> |
| |
| <p>For outgoing messages, the two JSON message formatters <code>JSONMessageFormatter</code> and |
| <code>JSONBadgerfishMessageFormatter</code> use Jettision to create an appropriate <code>XMLStreamWriter</code> |
| and then request Axiom to serialize the body element to that <code>XMLStreamWriter</code>. If a databinding |
| is used, then the body element will typically be an <code>OMSourcedElement</code> with an <code>OMDataSource</code> |
| implementation specific to that databinding. <code>OMSourcedElement</code> will delegate the serialization |
| request to the appropriate method defined by <code>OMDataSource</code>. This means that the databinding code |
| directly writes to the <code>XMLStreamWriter</code> 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 <code>OMSourcedElement</code> |
| backed by a corresponding JSON <code>OMDataSource</code> 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> |
| </section> |
| </section> |
| <section> |
| <h2><a name="Tests_and_Samples"></a>Tests and Samples</h2> |
| <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> |
| </section> |
| <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> |
| </section> |
| <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 <code>samples/quickstartadb</code> 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 <code>conf/axis2.xml</code>. |
| Add the following element to the <code>messageFormatters</code>:</p> |
| |
| <div> |
| <pre> |
| <messageFormatter contentType="application/json" |
| class="org.apache.axis2.json.JSONMessageFormatter"/> |
| </pre></div> |
| |
| <p>Also add the following element to the <code>messageBuilders:</code></p> |
| |
| <div> |
| <pre> |
| <messageBuilder contentType="application/json" |
| class="org.apache.axis2.json.JSONOMBuilder"/> |
| </pre></div> |
| </li> |
| |
| <li> |
| |
| <p>Edit the <code>services.xml</code> for the stock quote service and add the following |
| configuration:</p> |
| |
| <div> |
| <pre> |
| <parameter name="xmlToJsonNamespaceMap"> |
| <mappings> |
| <mapping xmlNamespace="http://quickstart.samples/xsd" jsonNamespace=""/> |
| </mappings> |
| </parameter> |
| </pre></div> |
| |
| <p>The <code>services.xml</code> file can be found under |
| <code>samples/quickstartadb/resources/META-INF</code>.</p> |
| </li> |
| |
| <li> |
| |
| <p>Build and deploy the service by executing the ant script in |
| <code>samples/quickstartadb</code> and then start the Axis2 server using |
| <code>bin/axis2server.sh</code> or <code>bin/axis2server.bat</code>.</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 '{"getPrice":{"symbol":"IBM"}}' http://localhost:8080/axis2/services/StockQuoteService</pre></div> |
| |
| <p>This will give the following result:</p> |
| |
| <div> |
| <pre>{"getPriceResponse":{"return":42}}</pre></div> |
| </section> |
| </section> |
| |
| |
| </div> |
| </div> |
| </div> |
| <hr/> |
| <footer> |
| <div class="container-fluid"> |
| <div class="row-fluid"> |
| <p>Copyright ©2004–2022 |
| <a href="https://www.apache.org/">The Apache Software Foundation</a>. |
| All rights reserved.</p> |
| </div> |
| </div> |
| </footer> |
| </body> |
| </html> |