Archive for November, 2014

Service Bus 12c- Mixing URI Parameters and Payload in REST Service

Typically, a PUT request URI looks like below accepting the actual payload in HTTP body.

http://host:port/employeesApi/Employees/{employeeId}

In Service Bus12c, We cannot mix URI parameters with representation of the resource, when we want to define a resource in this way.

put

If we run Proxy Service with this setup, we will get an error as shown below.

put1

put2

put3

Instead, you can use the expression as shown below.

put4

You can access this using the following expression from your message flow.

$inbound/ctx:transport/ctx:request/tp:user-metadata[@name=”empid”]/@value

If you log $body and above expression, you would see the log messages as shown below.

put5

put6

As explained in this link, you may have to populate these URI parameters in outbound variable.

Advertisement

Service Bus 12c – REST enabled Business Services

In this post, you will learn to create REST enabled business services. We will create business service to call the Proxy Service created in previous post.

You can create Business Services directly using WADL if it’s available, or using REST binding wizard similar to Proxy Services. As mentioned earlier, REST Business services convert payload from SOAP to REST i.e. the internal interface is still based on WSDL, while the external business and proxy services expose REST endpoints.

Using WADL:

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

restcmp_thumb2

bizrest1

Give name as EmployeeBS and Base URI and choose the option as shown below in Operation binding.

bizrest2

Select the WADL as shown below. You can also observe that you can choose resource from several places including MDS.

bizrest3

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

bizrest4

Click OK to finish the Business service creation.

Using REST Binding:

The steps for creating REST binding for business service are similar to Proxy Service. So follow the steps mentioned in section Creating REST Binding of previous post except giving the base URI.

Testing:

Run the business service and you will observe both SOAP and REST interfaces as shown below along with available operations.

testbiz

testbiz1

/Employees Resource:

GET:

getxml

getxml1

getjson1

getjson2

POST:

postxml1

postxml2

postjson1

postjson2

PUT:

putxml

putxml2

putjson1

putjson2

/Employees/{employeeId} Resource:

GET:

getxml2

getxml3

getjson3

getjson4

DELETE:

deletexml

deletexml.1jpg

deletejson

deletejson2

Similarly, you can run SOAP interface of business service by choosing operation and observe the output.

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


Pages

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

Join 379 other subscribers

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


%d bloggers like this: