About the RESTful Web Service Using JSON sample

The RESTful Web Service Using JSON sample demonstrates how to use some of the JSON parser features provided in IBM Integration Bus. The sample uses a REST interface to a WebSphere MQ application. The WebSphere MQ application uses XML messages to manage a list of compact disc (CD) data.

The message flows

This sample consists of three main components.

For each of the message flows, a test client of the same name drives the flow.

The provider message flow

The following diagram shows the structure of the web service provider for this sample, HttpJsonRestProvider.msgflow, in the JSONRESTSample Integration project.

A screen capture of the RESTful Web Service Using JSON sample provider message flow.

Section A

The HTTPInput node, HTTP JSON Input, listens for a message and has the Parse Query String property selected. If the request URI contains a query string (used by JSONP), the query string is stored in the local environment. The HTTPInput node also has the Set destination list property selected, which adds the HTTP method to the route to label destination list in the RouteToLabel node, RouteTo REST method. The route to label destination list is used later in the message flow.

The next node is a Compute node, StoreHttpReqId. This Compute node saves the target, which it extracts from the last part of the request URI. For example, http://localhost:7080/jsonrequest/target. This Compute node also saves the HTTP request ID. The target and HTTP request ID are saved in Environment, for use later in the flow.

Section B

Depending on the HTTP method of the input message, the message is passed to either a JavaCompute node or Compute node.

Though the function of these nodes is comparable, they demonstrate how to access the JSON tree, and how to convert the message to another data format (XML) by using Java or ESQL.

For more information about using different languages, including PHP, to access JSON, see Modifying a JSON message in the IBM Integration Bus documentation.

Section C

The message is then put to a queue, which is processed by the back-end WebSphere MQ application. The back-end WebSphere MQ application contains an MQOutput node, MQOutput (APP_IN1), that sends a message to the Compute node, WriteHttpReqId. The WriteHttpReqId Compute node builds a message that contains the local environment variables that were created earlier in the message flow. The MQHeader node, MQ Header, puts this new message onto the queue, STOREQ1, by using the MQOutput node, MQOutput (STOREQ1).

Section D

The MQInput node, MQInput (APP_OUT1), retrieves the message from the WebSphere MQ application. The MQGet node, MQGet (STOREQ1), retrieves a correlating message from the queue, STOREQ1. If an error occurs, it is caught by the Throw node, ThrowRestoreHttpReqIdError, which is used to to force an error path through the message flow if the content of the message contains unexpected data. The Compute node, JSON Reply, stores the request identifier in the local environment, which is used by the HTTP Reply node, HTTP JSON Reply. If the original HTTP request contains a query string, the query string is used as the padding value in a JSONP message.

For more information about providing and consuming JSONP services, see JSONP support in the JSON domain in the IBM Integration Bus documentation.

The consumer message flows

This sample contains two separate message flows that can consume the web service and JSONP consumer message flow.

WebSphere MQ JSON consumer

The first message flow, MqJsonClientConsumer.msgflow, uses JSON over WebSphere MQ, and shows how to invoke an HTTP REST service from WebSphere MQ by using a specific input queue to define the REST method.

A screen capture of the JSON consumer message flow.

This message flow contains four MQInput nodes; one for each of the REST methods:

The Compute node, BuildRestRequest, takes the message from these queues and builds a request to send to the RESTful service. In accordance with the REST architecture, this Compute node also extends the target URL with the name of the object upon which the request acts. The Compute Mode property of this node is set to LocalEnvironment And Message, so that the target URL and HTTP method, which are stored in the local environment, are passed through the message flow.

The HTTPRequest node, HTTP REST Request, passes its message to the RESTful service. The message that is returned by the RESTful service is dependent on the method that is being called.

The Compute node, RestoreMQMD, then restores the MQMD header to the output message and sends it to the JSON_REPLY1 queue by using the MQOutput node, MQ Output (JSON_REPLY1).


WebSphere MQ XML consumer for JSON REST service

The second message flow, MqXmlClientConsumer.msgflow shows how to consume an HTTP RESTful JSON service from a WebSphere MQ XML client, how to convert XML to JSON, and how to make an appropriate REST method call.

A screen capture of the XML consumer message flow.

This message flow contains a single MQInput node, MQ Input (XML_CONS_IN1), through which all of the REST methods pass. Because the web service requires a JSON message, the Compute nodes, XML-JSON and JSON-XML, transform the message from XML to JSON and JSON to XML appropriately.

The XML-JSON Compute node runs similar tasks to the BuildRestRequest Compute node, described in the WebSphere MQ JSON Consumer section. However, instead of using a different queue for each HTTP method, one queue only is used, and the type of HTTP method to use is stored as a value in the XML message. This node also sets the member element as a JSON array with the following code:

SET OutputRoot.JSON.Data = InputRoot.XMLNSC.Collection.CD;
DECLARE membersArray REFERENCE TO OutputRoot.JSON.Data.Members;
IF LASTMOVE(membersArray) THEN
	-- Format the Members element as a JSON Array
	SET  membersArray TYPE = JSON.Array;
END IF;

For more information about using different languages, including Java and PHP, to modify and transform JSON messages see Modifying a JSON message in the IBM Integration Bus documentation.

JSONP Consumer

The JSONP Consumer is a web front end that uses JavaScript to access the provider directly. The following diagram shows the result of the JavaScript in JSONPConsumer.html:

A screen capture of the JSONP script.

You can change the query by modifying the following HTML JavaScript tag:

<script type="text/javascript" src="http://localhost:7080/jsonrequest/*?jsonp=parseResponse"></script>
For example, changing the query to the following code returns any CD with the title "IX":
<script type="text/javascript" src="http://localhost:7080/jsonrequest/IX?jsonp=parseResponse"></script>

For more information about JSONP, see Providing a JSONP service and Consuming a JSONP service response in the IBM Integration Bus documentation.


The back-end application message flow

The following diagram shows the back-end WebSphere MQ application message flow, MqXmlApp.msgflow:

A screen capture of the back-end WebSphere MQ app.

This flow contains an MQInput node, MQ XML Input (APP_IN1), and a Compute node, Application, which initializes the sample data on its first run. This data is stored in a long-lifetime variable, which persists for the duration that the flow is deployed. The Compute node also performs a create, retrieve, update, or delete (CRUD) function on this data, depending on the content of the XML message. Finally, the message is propagated to a WebSphere MQ queue, APP_OUT1, on the MQOutput node, MQ XML Output (APP_OUT1).

The messages

The consumer message flow is driven by a WebSphere MQ message. A test client file is supplied that runs the sample by using the following messages:

Retrieve

When the following message is sent to the RETRIEVE_IN1 queue, the data about all the CDs is returned:

{
 "Title" : "*"
}

When the following message is sent to the RETRIEVE_IN1 queue, CD data is returned for any CD with the title "Friars Ball".

{
 "Title" : "Friars Ball"
}

Both of the previous messages return a message in the following format:

{"Title":"Friars Ball","Artist":"Deep Blue","Country":"UK","Price":"9.90","Year":"1971","Members":
["Liam Gillan","Alan Blackmore","Phil Glover","David Paice","Tom Lord"]}

Delete

When the following JSON message is sent to the DELETE_IN1 queue, the data for any CD with the title "Friars Ball" is deleted.

{
 "Title" : "Friars Ball"
}

If successful, the following message is returned:

{"Status":"Success"}

Create and update

If the following message is sent to the UPDATE_IN1 queue, the data for any CD with the title "Friars Ball" is changed; the updated record only is returned. If the message is sent to the CREATE_IN1 queue, a completely new entry is created; all of the data is returned, including the new entry.

{
 "Title" : "Friars Ball",
  "Artist" : "Deep Orange",
  "Country" : "United Kingdom",
  "Price" : 19.90,
  "Year"  : 2010,
  "Members":["Liam Gillan","Alan Blackmore","Phil Glover","David Paice","Tom Lord"]
}

Back to sample home