Advertisements



Service Bus 12c – REST enabled Proxy Services

Note: This post is based on SOA 12.1.3 release.

In this post, you will learn to expose Proxy Services as REST services. We will use Employees table and come up with REST API that can be used to perform CRUD operations.

This post assumes knowledge of DB connection pool setup and Pipelines.

Creating Business Service

Create the business service using DB Adapter with the help of following steps.

dbadapter1

dbadapter2

dbadapter3

dbadapter4

dbadapter5

dbadapter6

dbadapter7

dbadapter8

dbadapter9

dbadapter10

dbadapter11

dbadapter12

dbadapter13jpg

dbadapter14

Click Finish and verify that business service has been created as shown below.

filexp

Refactor all the artifacts to BusinessService directory.

filexp1

Now you can test business service alone to make sure that all operations are working fine as expected. You can refer to this post for detailed steps.

Creating REST Binding

We want our REST resource to support the following operations:

Resource

HTTP Verb

Resource URIs

Desription

Employees GET /Employees Get all employees
GET /Employees/{employeeId} Get an employee represented by Employee ID.
POST /Employees Create new employee
PUT /Employees Update new employee
DELETE /Employees/{employeeId} Delete an employee represented by Employee ID.

In Service Bus 12c, REST proxy services convert REST native payload to SOAP before invoking a pipeline or split-join, while REST business services convert payload from SOAP to REST. That means, the internal interface is still based on WSDL, while the external business and proxy services expose REST endpoints. This will be evident in later steps as service bus creates both WADL and WSDL files and each resource is mapped to different operations defined in WSDL.

Drag REST binding into Proxy Services swim lane in Service Bus overview editor as shown below.

restcmp

proxy1

This brings up Create REST binding window. Give the name as shown below.

restbind1

Click + icon for Resources to add new resource and click OK.

restbind2

Similarly add another resource /Employees/{employeeId}. Now your resources should look like below.

restbind3

Now you have to create the corresponding Operation Bindings which in turn creates WSDL with these operations.

Operation Bindings for resource /Employees:

Use the following steps to create GET mapping.

restbind4

get1

get2

Here we are selecting both XML and JSON as we want our API to support both content types. Click on highlighted icon to define schema on fly representing the output payload. Enter file name and give directory name as ProxyService as shown below.

xsd1

Click Next and choose file type as JSON.

xsd2

Click Next and give Target namespace and Root element as shown below and you can use the following JSON payload sample.

{
“Employee” : [{
“employeeId”: “100”,
“firstName”: “fname”,
“lastName”: “lname”,
“email”: “fn@gmail.com”,
“phoneNumber”: “9848012345”,
“jobId”: “100”,
“salary”: “100”,
“commissionPct”: “100”,
“managerId”: “100”,
“departmentId”: “100”
},
{
“employeeId”: “101”,
“firstName”: “fname1”,
“lastName”: “lname1”,
“email”: “fn1@gmail.com”,
“phoneNumber”: “9848012345”,
“jobId”: “100”,
“salary”: “100”,
“commissionPct”: “100”,
“managerId”: “101”,
“departmentId”: “101”
}]
}

xsd3

Click Next and verify the schema that gets generated.

xsd4

Click Next and Finish and verify that schema URL is populated in Response tab.

restbind5

Click OK and verify Operation Bindings are populated as shown below.

restbind6

Use the following steps to create POST mapping by reusing existing schema.

post1

post2

post3

post4

post2

post5

Similarly, finish mapping for PUT and use operation name as updateEmployee. Now your Create REST binding window should look like below.

3ops

Click OK. You will observe that Proxy Service, WADL and WSDL have been  created as expected.

filexp2

You will also observe errors, as XSD file is created in ProxyService folder whereas WADL and WSDL got created in Resources folder. So open WADL and WSDL files and modify the XSD reference as shown below to resolve errors.

wadl

wsdl

Operation Bindings for resource /Employees/{employeeId}:

You should reuse the schema created in above steps for this resource as well.

Now in Service Bus overview editor, right click EmployeeService and select Edit REST as shown below.

editrest

Use the following steps to create GET mapping.

rsrcget5

empkey1

You can leave schema for request as it is so that it will be defaulted by the wizard.

rsrcget3

post2

rsrcget4

Click OK and your Update Service window should look like below.

rest1

Similarly finish DELETE method by using operation name as deleteEmployee and now your Update Service window should look like below.

allbinds

Click OK and observe the WADL and WSDL files that got updated with your new operation bindings. If you observe that WADL is not updated just do refresh as shown below.

refresh

WADL:

wadl1

WSDL:

wsdl1

 

With this, you have finished the REST interface that is exposed to consumers.

Creating Pipeline:

Create new pipeline EmployeePipeline using EmployeeService.wsdl as shown below.

pipeline1

pipeline2

Connect EmployeeServcie proxy service with EmployeePipeline in Service Bus overview editor as shown below.

soverview1

We will not discuss much about finishing the message flow in pipeline as it’s nothing different from regular scenario. You can find the Service Bus project here i.e. used for demonstration purpose in this post.

Testing:

In test console, you can observe that Accept http header shows both application/xml and application/json i.e. now your REST service supports both xml and json data formats.

GET With application/xml:

gettetsing1

gettetsing2

GET With application/json:

gettetsing3

gettetsing4

POST With application/xml:

posttest1

posttest2

POST With application/json:

posttest3

posttest4

You can observe that both records have been inserted as expected.

posttest5

PUT With application/xml:

puttest1

puttest2

PUT With application/json:

puttest3

puttest4

You can observe that both records have been updated as expected.

puttest5

Resource /Employees/{employeeId}:

GET With application/xml:

getkeytest1

getkeytest2

GET With application/json:

getkeytest3

getkeytest4

DELETE With application/xml:

deltest1

deltest2

DELETE With application/json:

deltest3

deltest4

You can observe that both records have been deleted as expected.

deltest5

If you want to expose SOAP interface as well, then you just need to create another Proxy Service and join EmployeePipeline as shown below.

soaprest

Notes:

  • We did not talk about fault handling specific details here.
  • It’s always recommended not to do blind GET as we may see performance issues when large number of records are there.
  • As an exercise, you can come up with resource emulating QBE on db.

References:

https://docs.oracle.com/middleware/1213/osb/develop/GUID-C346DF7D-041D-4E10-BE1C-451F50719106.htm#OSBDV88210

Advertisements

18 Responses to “Service Bus 12c – REST enabled Proxy Services”


  1. 1 Tony Peterson June 24, 2017 at 12:37 AM

    Hello, Siva,

    Nice article. I have a unique requirement that I wonder if you could tell me if it is possible.

    I need to use a URI parameter to do a PUT to update a resource on a REST service. However, I do NOT want to submit that parameter as part of my request. Can you tell me if that is possible?

    So, in short, I’m needing to specify an ID parameter on my URI…but do NOT want to use it in the JSON input.

    Thank you for any light you could shed on this issue.

  2. 3 Nagaraj December 5, 2016 at 4:26 PM

    Very nice article indeed. It would be good if you can throw light on how can I expose this REST URI to external clients so that it can be consumes by a mobile application or tested in a client like SOAPUI?

    • 4 svgonugu December 5, 2016 at 5:45 PM

      typically in your local dev, you don’t require any setup. Directly the OSB rest service urls can be used and tested using SOAP UI. But in the actual environments (Dev, Test or Prod), one would always have OHS or some load balancer before OSB server and this load balancer will have the url mappings so the exact host and port details will not be revealed to customers. External clients will always be hitting this load balancer.

      Same is the case when you want to access it from mobile. But local dev access is not straight forward and may need to create some LAN and all so that you can access it from mobile

  3. 5 Anju November 7, 2016 at 11:32 PM

    Hi Siva,

    You have a very informative blog! It really help folks like us.

    I am using REST binding (12.2.1), and have 2 flavors of GET operation as below

    resource path
    /employee
    /employee/{id}

    When we actually define the logic for the GET URI parameter operations how do I access {id} within a pipeline ? Is there any variable or property I can refer to ?

    I used inbound/ctx:transport/ctx:request/http:relative-URI , but it gives the complete URI – /employee/{id} , I can remove the ID value but was wondering if there was any inbuilt parameter

    Thanks,
    Anju

  4. 6 kuttimahesh August 26, 2016 at 2:54 AM

    Could not execute exact steps from Creating REST Binding section in 12.2.1

    from below step cannot continue

    Here we are selecting both XML and JSON as we want our API to support both content types. Click on highlighted icon to define schema on fly representing the output payload. Enter file name and give directory name as ProxyService as shown below.

  5. 8 adarshkkumar April 19, 2016 at 5:56 PM

    Hi Shiva ,

    can we validate JSON payload using XSD like we do for XML payload, using Validate Action ?

    Regards
    Adarsh

  6. 11 Subhash Venkata January 28, 2016 at 7:06 PM

    Hi Siva,

    Thanks for the good article.

    I have a requirement currently i am working on, I need to pass the JSON input payload for GET request instead of query parameter.

    Is there any way to achiev this in OSB 12c REST adapter?

    Please help

    -Venkat
    9490351358

  7. 13 Suresh December 4, 2015 at 9:05 AM

    Hi Siva,

    Thanks for the response. Sure I will try this.

    Suresh

  8. 14 suresh August 13, 2015 at 4:04 PM

    Hi Siva,

    Can you help me how to read XML file and write file with JSON output ? is it possible in 12c??

    thanks
    Suresh

    • 15 svgonugu August 13, 2015 at 4:41 PM

      It should be possible though i don’t have working example. You can achieve this using nxsd schema using steps similar to below.
      – Created nxsd schema from File -> New.
      – There you will see JSON format as the option and proceed to next page.
      – give the options and come up with sample JSON payload matching your xml structure and give it there in source.

      After nxsd gets created, it should be just using translate activity to transform XML payload (source schema) to JSON payload (target schema).

    • 16 svgonugu August 13, 2015 at 4:41 PM

      It should be possible though i don’t have working example. You can achieve this using nxsd schema using steps similar to below.
      – Created nxsd schema from File -> New.
      – There you will see JSON format as the option and proceed to next page.
      – give the options and come up with sample JSON payload matching your xml structure and give it there in source.

      After nxsd gets created, it should be just using XSLT to transform XML payload (source schema) to JSON payload (target schema).


  1. 1 Service Bus 12.2.1 – REST Support | Siva's Blog Trackback on July 4, 2016 at 3:22 PM
  2. 2 Service Bus 12c – Series of Articles | Siva's Blog Trackback on November 14, 2014 at 3:33 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Advertisements

Pages

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 337 other followers

Enter your email address to follow this blog and receive notifications of new posts by email.