Wednesday, September 13, 2017

OSB - Consuming REST services [Part 1]

In this post I'll talk about using the REST adapter in OSB 12c to consume a sample webservice:
http://jsonplaceholder.typicode.com. This REST API uses JSON format so we will also use the native format wizard to create the XSD.

GET Operation - Template Parameter
We'll start with a GET operation for the following resource:

/posts/1/comments

This involves using a template to specify the post ID. 

First, drag the REST adapter into the External Services lane to open the Create REST binding window. The Base URI is the one mentioned at the beginning of the post.

Under the Resources section, click the + icon to add the following Relative Path/posts/{id}/comments


We add the brackets and a keyword (in this case, id) to specify the parameter in the style of a template according to how the API expects the resource to be formatted.

Next, in the Operation Bindings section, click the green + and choose Add operation binding to open the REST Operation Binding window. Here, specify the name of the Operation (getPosts), and the resource is /posts/{id}/comments. Because this is a GET operation, the HTTP Verb is GET.


In the Response section, check the JSON box and then click on the Define Schema for Native Format icon on the right side. This is where we will define the WSDL for the operation as well as the schema for the JSON format that is returned by the REST API.

In the Native Format Builder window, provide a proper File Name to the schema and click Next. The File Type is JSON Interchange Format, and in the next step we will choose the filename that has a sample on how the API returns the response:


It will generate a Native Format Schema File like so:



In the Request tab, leave as is. In the URI Parameters, it should show the id Parameter from the template that we defined earlier. Once you click OK, the Request and Response tabs should look as follow:

Request

Response

Now that we've defined the REST service to consume, we will expose a SOAP Webservice that takes a string as an input and another string as an output. I'm not really concerned about the output of the proxy webservice since we just want to see that OSB is consuming the REST service properly given a string as a parameter (which will represent the id that it needs to send).



So create a Proxy service with its pipeline using this schema. The OSB project should look like this:


In the Pipeline, add a Route activity and within, a Routing activity. In the Request Action, add a Replace activity. Here we will send the input parameter to the id element that we defined earlier as a template parameter:



Make sure to select Replace node contents instead of Replace entire node. Once done, we can deploy and test in the Service Bus Console!

Sending a 1 as the input parameter should return a list of comments for the post with Id 1:




GET Operation - Query Parameter
In the first part we did an example of sending a GET request using a template. Now we will see how to do a GET request using a query parameter for the following resource:

/comments?postId=1

Right-click the REST adapter and select Edit REST. Under the Resources section, click the + icon to add the following Relative Path/comments

Next, under Operation Bindings, click Add operation binding. Name the operation getPostsQuery and HTTP Verb as GET. In the Request tab, click the Define schema for Native format to specify that the postId will be one of our query parameters. In the Native Format Builder wizard, choose URI Sample and click Next.

Here, provide the following URL as the sample:
http://jsonplaceholder.typicode.com/comments?postId=1


Click Next twice and then Finish. You'll see that the URI Parameters section has now been updated (the Expression column will update once we click OK):


In the Response tab, use the same NXSD that we built for the previous example since the response will be the same, it's just the request format that is different.

In the pipeline, update the Replace activity accordingly with the updated XML parameters:


Don't forget to also update the operation to getPostsQuery (or whatever you named it) in the Routing activity.

Deploy and test. You should get the same results as in the previous exercise.

And that's it. Super simple to consume a REST JSON service using the REST adapter.

In the next part I'll cover the POST operation. 

14 comments:

  1. Hi, just wondering if you still have this example somewhere. I cant seem to make it work.

    "In the Pipeline, add a Route activity and within, a Routing activity. In the Request Action, add a Replace activity. Here we will send the input parameter to the id element that we defined earlier as a template parameter"

    In the replace activity what are we replacing?

    Thanks

    ReplyDelete
    Replies
    1. Sorry for the late reply, I didn't get a notification for this comment. In the replace activity you replace the body that you're sending out with the parameters that the Rest request requires, in this case, the ID.

      Delete
  2. 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
  3. Hi,
    I have a business service Rest adapter.. I have to pass path parameter value which is stored in a variable in pipeline.. how do I pass this variable value in rest adaptor?
    Its really urgeny.. can you please help me with this?

    ReplyDelete
    Replies
    1. Use the replace activity to replace the body with the request that the REST service needs. If you look at what I posted:
      "In the Request Action, add a Replace activity. Here we will send the input parameter to the id element that we defined earlier as a template parameter:"

      ...you'll see that in the image that follows the above statement, I'm using the $body variable. Here you could use whatever variable you have declared in your pipeline.

      Delete
    2. I needed this for POST operation but in the blog you have specified GET

      Delete
    3. Should I keep the expression column under URI parameters pane in rest adapter empty??

      Delete
    4. I haven't worked with POST REST requests in a while but it should be the same. When you generate the NXSD for the POST body and parameters, you should be able to specify with the Replace activity what those parameters should be. Sorry I can't be of much help now.

      Delete
    5. And trick here is I should not pass the path patamepath value in the post body
      Anyways thanks a lot.. let me know if u get any info about tbis :)

      Delete
  4. It would be great if you could help..

    ReplyDelete
  5. You skipped the response JSON; just mentioned that the file json_sample.json should be used. Opening the URI and copy/pasting the actual response into the sample window fixed that. Same for the request; I pasted the URI there to generate the WSDL. But then you completely skipped over how to create the pipeline. What to select for "Location" and "XPath"? Where did the URL for the xmlns in the Request Action come from? Pasting the gives an error that the prefix "rest" cannot be resolved. It would help immeasurably if you can add the missing screens in this blog.

    ReplyDelete
  6. Hi, do you have the source file? Could you share it?

    ReplyDelete
  7. In my JDeveloper 12.2.1.3 in Response section I do not have the "Define Schema for Native Format" option, is related to my JDeveloper version?

    ReplyDelete

  8. Thanks for the nice blog here.I was searching this one for a long time.This blog is very helpful for my studies..I got another one site also,which is same as yours Oracle OSB.Check this one also Oracle Fusion HCM Sure it will be helpful for you too..Once more iam thanking you for your creative blog.

    ReplyDelete