Monday, September 9, 2013

Creating REST Services in OSB


In my previous post Rest Support from OSB I have explained on how to consume the HTTP post multi mime REST service in OSB . This post describes on how to create REST services in OSB for basic native HTTP methods GET & POST .


Part I : Creating a proxy service for Native HTTP methods (GET/ POST) and Testing :
1) Decide on how the query parameters will be passed to the GET service , there are two approaches first using relative URL parameters or using the Query string. I prefer the query string approach where you can pass multiple query parameters , like :
http://localhost:7001/LoanRestServices/Proxy/GetLoanService?BorrId=101&LoanId=9001

The value of these parameters can be fetched inside the proxy flow using xpath :
$inbound/ctx:transport/ctx:request/http:query-parameters/http:parameter[@name="BorrId"]/@value
$inbound/ctx:transport/ctx:request/http:query-parameters/http:parameter[@name="LoanId"]/@value


2) Create a Proxy Service : GetLoanService
Type : Any XML Service

3) Implement message flow

3.1 you can create separate services for each type of operation or can use conditional Branch, here is how the flow should look like using the conditional branch:




The conditional branch will be based on inbound variable value :
./ctx:transport/ctx:request/http:http-method/text() = 'GET' or 'POST'

3.3 For GET flow fetch the value from query parameters  and use them to invoke other downstream services or just create a static response.

3.4 For POST flow fetch the input parameters  value using simple Xpath like : $body//LoanId/text() and use them to invoke other downstream services or just create a static response.

4) Testing the GET and POST service , execute the proxy from service bus console .

4.1 For GET
In Transport section populate below values :
http-method: GET
query-parameters:
<tp:query-parameters xmlns:tp="http://www.bea.com/wli/sb/transports/http">
<tp:parameter name="LoanId" value="9001"/>
<tp:parameter name="BorrId" value="101"/>
</tp:query-parameters>

4.2 For POST

In Transport section :  http-method: POST
In payload pass :
<Loan>
<LoanId>8001</LoanId>
<BorrId>10233</BorrId>
</Loan>

Execute. This runs perfectly fine.

 Part II : Creating a Test service to test the Rest service created in previous step to do testing without using a test console and see how other services can consume the REST service.

1) Create Two Proxy services :
InvokeLoanService
InvokeLoanServicePOST for invoking GET and POST operation respectively.
Type : Any XML service

2) Implement the Proxy Flow :

2.1 ) Fetch the input values using  simple Xpath : $body//LoanId/text()

2.2) Add a Route Node to invoke the service created in Step I.
- Add Insert for adding the Method Type for invoking the service :
Insert <http:http-method>GET</http:http-method> for Xpath ./ctx:transport/ctx:request in Outbound

- Add Insert for adding the Query parameters like :

Insert <http:query-parameters/> for Xpath ./ctx:transport/ctx:request in Outbound
Insert <http:parameter name="LoanId" value="{$LoanId}"/> for Xpath ./ctx:transport/ctx:request/http:query-parameters in Outbound
Insert <http:parameter name="BorrId" value="{$BorrId}"/> for Xpath ./ctx:transport/ctx:request/http:query-parameters in Outbound

2.3 ) For Post service , We just need to have one insert for the method type , like :
Insert <http:http-method>POST</http:http-method> for Xpath ./ctx:transport/ctx:request in Outbound
2.4 ) For Post service replace the body in route to pass required values like:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
    </soap:Header>
    <soap:Body>
<LoanId>{$LoanId}</LoanId>
<BorrId>{$BorrId}</BorrId>
</soap:Body>
</soap:Envelope>

  The Route will look like :



 You can simply test these services using payload, no need to set the query parameters and HTTP method as that is handled internally by the services.
<Loan>
<LoanId>102</LoanId>
<BorrId>2345</BorrId>
</Loan>

Cheers !!



2 comments:

  1. hi,
    thanks for your post.It helped me.

    how to convert json response to soap in osb

    ReplyDelete
  2. how send parameters and payload using PATCH verb?

    ReplyDelete