Tuesday 5 May 2015

REST OSB SOA 12C Integration using JSON

In this Blog, I will discuss how Oracle Service Bus (OSB) 12c can be used to transform enterprise system interfaces into a mobile-optimized REST-JSON API. In this Blog, we will Create an application, which will consume JSON message from MAF using REST adapter in OSB 12C. OSB will SOA service called business process and SOA will ultimately invokes the database or backend. Thus, it involves an integration of MAF -> OSB -> SOA -> Backend(data Layer)

But how to do it boss. We will start building components using bottom up approach. See our requirement is very simple. We want to fetch Employee Data (name and Status) based on EmployeeId. From mobile application user will enter employeeID and click on search. Request will hit OSB. OSB will get JSON input and after transformation to respective xml message, sends it to SOA Layer. SOA will consume xml message and will call database where employee records are placed. And will retrieve the employee record based on EmployeeID passed. OSB will again convert xml response to JSON response and sends back to Mobile application.

So first thing here is to create database table and put some employee records.

  • Create Employee table




  • After Creating Table, we need to create SOA Service which will use database adapter to Fetch Employee record based on EmployeeId.






  • We have used Mediator service to call Database Adapter. FetchEmployeeDetails is database adapter which is used to call Employee table.

 We have completed one half part of the usecase. Let’s test it and note down the results.

Request:-



Response:-




Note down the Endpoint of this SOA Service




This is simple man, why I’m wasting my time in reading your blog.   I already knew this..
Wait, wait. Actual part is coming now, where we are gonna consume this SOAP/xml service in OSB and will expose it as REST Get .






 Create OSB Application and OSB project as below





Right click on the Proxy Services swimlane and Choose REST option.



In the Create REST Binding window, give the relevant name to service.   Example:- RESTEmployeeDetailsService


Also, We need to choose relative Resource Path. Double click on Resource Path.


Here , we have choose relative path as /EmployeeId/{EmployeeId}.  By putting it in {} braces, means it will be passed with actual value of EmployeeId in the request URL. The actual URL which user needs to call will become like highlighted in below screenshot.  Example:-

will define <proxy-uri> later



  • After defining Resource path,  we need to define Operation Bindings. For that ,we need to click on + sign and choose the option ‘Add Operation Binding’






  • Give the relevant name on Operation field and choose the HTTP Verb as below.




  • Click on the response tab and choose the format as JSON. Also click on highlighted button to  create the native schema for JSON response




  • Give the File Name nxsd_GetEmployeeDetails.xsd(Any Relevant name)for xsd and click next






  • Choose the ‘JSON Interchange format’ and click next







  • Enter a sample JSON Payload for the response.  Now, where to get this. I don’t know how to get JSON format for the SOA service response.  










  • Copy the JSON response sample  and paste it Sample section of below window:-





  • Jdeveloper will create Native Schema file for sample JSON format as below:-





  • After clicking OK, the control will return to below page and will create nxsd_GetEmployeeDetails.xsd in the Jdeveloper Project.




  • Operation has been created as below and click OK.




  • After that we need to create Pipeline in OSB by right clicking on Pipeline/Split-Join swimlane and choose Pipeline as below





  • Select wsdl option and click on browse to choose REST wsdl created while performing above steps:-





  • Choose the wsdl ‘RESTEmployeeDetailsService.wsdl’ and click OK




  • Click Finish and it will create Pipeline in the Project.





  • After that we need to create Business service in OSB to call SOA Service by right clicking on External  Services swimlane and choose Business Service as below





  • In the selection field,  enter the end point of SOA service, which we have noted earlier and click ok.




  • Click Next





  • Click Finish





  • OSB composite will look like below:-




  • Now we need to create transformation of Source data to Target data as both have different formats. We can achieve this with the help of Xquery in OSB.
  • Let’s first create xquery for source (Request) transformation.
  • Create new xquery resource and give it any relavant name and click on Green + sign in the source section.




  • After that click on the edit button as below steps:-






  • Since, we are getting EmployeeId as string , so choose string from the Type Choose and click ok





  • Now comes the target section. Here we need to  call SOA service. So, we want to convert our string input to the Target SOA Service input. So  choose below highlighted type as it is derived from SOA service xsd.


















  • After that we need to use Replace activity to apply input transformation in the pipeline as below:-




  • Similarly, we will create xquery Response Transformation which will convert SOA service response format to the OSB REST output. Please perform as below:-















After deploying the code on OSB server, we need to test it.  There are many ways to test it.  However, we have covered 2 below ways:-
·        Testing it from Browser
·        Testing it using Advanced REST Client
How to get the full endpoint to test it. So,  Please check the proxy service endpoint






So the full endpoints will be :-





Testing it from Browser:-






Testing it from Advanced REST client:-