Thursday, 3 October 2013

REST service in OSB - Creating a WSDL Proxy for REST Service

In this post we will see how to consume a RESTful service in OSB and create a WSDL proxy for the same so that that the WSDL clients can consume.

Steps:
1. Consuming the REST Serivce in OSB and creating a business service.
2. Creating a WSDL proxy and connecting routing it to the REST business Service.


Consuming REST service in OSB:

1. Create a Oracle Service Bus configuration Project in OEPE- eclipse.


2. Create a Oracle Service Bus Project


3. Create folders business services, proxy services, jars, wsdls under project.

4. Create a Business service under business services folder.

5. Select Messaging service in the General tab and select the request and response message types in as per your requirement.





6. Add the Rest service url in the Transport tab.

7. Leave the remaining tabs with defaule values.
8. You can test the busieness service(if you want) by exporting the project from eclipse and importing in OSB console.

Creating WSDL Proxy for REST service:

1. Create a WSDL which will be used to create the proxy for the REST business service.
2. The WSDL should contain the input, output and operation that it exposes. Please find the below WSDL for reference (it show only the operation and bindings).



3. Create a new proxy service and select Service Type as WSDL web service and browse and select the WSDL that is created in the previous step.




4. Leave all other tabs/fields as default values and click on messag flow tab.

5. Create a conditional branch under the proxy service and set the condition for branch as the method argument from the input variable. (in the example that is depicted here the http method name is passed in the input)



6. Add a branch and for handling GET requests.



7. Add a Route node under GET brancha and pipeline pair which will route to the REST business service which is created earlier.



8. Now we have to prepare the request pipeline to invoke the REST service. The REST url for the GET method will be http://hostname:port/context/event/1 where /1 is the relative uri which will fetch the event details with event id 1.

9. So we need to set http-method (value GET), and relative-URI value (the id value that is passed to the WSDL proxy service.

10. Add an insert action which will set the http-method.



11. Add an assign action which will get the id from the wsdl input and set it to a variable (id).



12. Set the relative URI.



If your REST service is expecting the query string( like http://hostname:port/context/event?id=1) instead of relative URI then set the query string as mentioned below, 

fn-bea:inlinedXML(concat('', concat('', 'id=',$id, ''), '
'))
13. Now the request part is completed. 

14. If your REST service returns the xml you can directly map it to the reponse of the proxy service(after performing required transformations).

15. In this example my REST service returns JSON response so i need to convert it into the xml format which we will see in the below steps.

16. In the response pipeline add an Assign activity which will assign the response body to a variable 'jsonRes'.



17. Now we need to convert the json reponse to xml format. For this we need the JSONXMLConverter.jar which has two methods to conver json to xml and vice versa. For this jar to work we need to copy following jar under the lib folder of your OSB domian.

  • commons-beanutils-1.8.3.jar
  • commons-beanutils-bean-collections-1.8.3.jar
  • commons-beanutils-core-1.8.3.jar
  • commons-lang3-3.1.jar
  • ezmorph-1.0.6.jar
  • json-lib-2.4-jdk15.jar
  • junit-4.10.jar
  • xom-1.2.8.jar
18. Add a java callout activity which will call the json2xml() in JSONXMLConverter.jar and pass the input as jsonRes and assign the output to 'xmlRes' variable.


19. Replace the response body node contents witht xmlRes.




20. Now we can test the proxy service from OSB console or from SOAP UI (we can get the exposed wsdl of proxy from OSB console).

21. We can configure the other http methods in the same manner.

7 comments:

  1. Very Nice article and Very useful

    Could you please provide me the Souce code for this,It would be useful for me

    ReplyDelete
    Replies
    1. Could you please provide me the Souce code for this
      My Mail Id : challanirmal@gmail.com

      Delete
  2. Very useful article. Can you please provide the code for the same.
    That would complete the gap.

    ReplyDelete
  3. Hi, I have successfully implemented the code in my application.I am getting JSON response in body.Now I am facing issue with JSONXMLConverter.jar. I am not able to find this jar in google.

    ReplyDelete
    Replies
    1. Hi Atul,
      Share me your mail ID, I can send you the jar file.

      Delete
  4. Hi,

    Nice post.. Can you please share the code to my id rajesh.kalvakota@gmail.com?

    Thanks,
    Raj

    ReplyDelete
  5. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingmulesoft online training

    ReplyDelete