Thursday, 8 September 2016

Enabling JSON based REST Invocation in SOA OSB 11g

Enabling JSON based REST Invocation in OSB 11g

Scenario: Service Provider is migrating from SOAP WS to REST Model. Service Consumer needs to be updated with REST Clients.
Solution: If all the communication happens via OSB Layer then SOAP messages can be translated to JSON messages so that Service Consumer don’t have to modify their WS clients.

In this post, we will discuss on how to leverage Java Callout capability in OSB to enable JSON based REST Invocation.
On a high level, OSB has two components called Proxy Service & Business Service

Proxy Service will define the service with message formats, communication protocols, Message Flow and target Business Service.
Business Service holds the information about Service Provider like URL, mode of communication, authentication mechanism, etc.
Service Consumer will have no idea about to which instance of Service Provider the request is redirected. It acts as an Abstraction layer, where service consumer & service provider were not tightly coupled.
Let’s come back to the scope, Enabling JSON based REST Invocation in OSB.
Ø  If the incoming request is REST message, then it is straight forward, you can define the proxy service’s service type as Messaging Service to accept the data as Text and pass it to business service where URL & corresponding http method will be configured, with which the request will be sent to the target Service Provider.
Ø  If the incoming request is SOAP message, then Java callout capability in OSB can be leveraged to convert the SOAP XML message to JSON message and pass on.
To perform any activity on OSB except read, you need to create a session which can be activated post your configuration is completed

Create Proxy Service:
1.       Under Create Resource Field, select Proxy Service.

2.       Expose the Proxy Service as SOAP Web Service so that SOAP based Service consumer will communicate to proxy in a seamless manner (In my case, Service Consumer was SOA BPEL Process).
Click Next, Select Protocol as http. Endpoint URI will be prepopulated, modify as needed.

3.       Click Last, Let’s skip to last page of this configuration leaving all other parameters with the default value. Then Click on Save button.

Before getting into Proxy Service Message Flow, let’s create the corresponding Business Service,

Create Business Service:
1.       Under Create Resource, select Business Service. Enter Service Name
2.       Select Service Type as Messaging Service for accepting json messages from proxy service. Click Next

3.       Select the Request Message Type & Response Message Type as Text and Click Next

4.       Select protocol as http and configure the Endpoint URI & add the same then Click Next

5.       Select the appropriate method as needed. If any basic authenication needed then configure a service account and tag here. Then Click Next.
Note: If you want to invoke GET method, then Request Message Type should be None, because GET method invocation cannot have a body. If you want to invoke HEAD method, then Response Message Type should be None.

6.       Click Last and save the configuration .




Linking Proxy Service to Business Service:
Now Proxy service & Business Service is created, In order to link the proxy service to business service, Click Edit Message Flow button to create a message flow for this proxy.

·         In the Message flow, add a Route to the service and click Edit Route


·         Click the link in the Route to field to configure the corresponding Business Service.
·         In the Request Action section,


External Activity: Develop an API to convert incoming XML object to JSON String as a static method which will take apache XMLObject as input param and return String as output. Upload the JAR file into OSB with all dependency jar files. Hint: Develop the api as Maven Project to manage dependencies effectively.
·         Click Java Callout to link to configure the static method in the api. Locate the JAR & implemented method.
ü  Configure Input Parameter as $body, so that the entire SOAP Body will be passed as input.
ü  Capture the return parameter (output) in result variable (run-time variable).


·         Add another action called Replace to replace the body content with temporary result variable
ü  Enter the variable as body
ü  Enter the with value as ‘$result’
·         Add another action called Replace to set the content-type of the request.
ü  Click XPath link and replace it with “./ctx:transport/ctx:request/tp:headers/http:Content-Type”
ü  Enter the variable as outbound
ü  Enter the with value as “<http:Content-Type>application/json</http:Content-Type>” or ‘application/json’
In case you are trying to perform GET operation, the default content type of business service is ‘binary/octet-stream’, so you need to update the content type in proxy service & send all the headers to business service.
·         Below screenshot will speak about setting up the Transport Headers (Add an action->Communication->Transport Headers/Routing options options) for enabling GET operation with respective Content-type. In case if you need to add some application data to URI for GET operation, then perform a concat operation using xquery & update URI field in Routing Options action. The updated URI will be overridden in the business service.

Likewise, perform the reverse activity to convert the incoming JSON message to appropriate XML format and set in the body content in the Response Action Section.


By this way you can enable JSON based REST Invocation in OSB Layer without impact the service consumer & service provider.

Recent Posts

Micro VMs & Unikernels

This post is a follow up of this post . In Previous Post, we discussed about Virtual Machines & Containers architecture. In this post, w...

Older Posts