Consuming a RESTful service from the OSB

In this recipe, we will show you how to consume an existing RESTful service from the OSB. We will reuse the service we have provided in the previous recipe and implement a proxy service/business service pair to expose it as a SOAP-based web service.

The business service is using the HTTP transport to invoke the RESTful service and a proxy service is exposing this as a SOAP-based web service, also using the HTTP transport.

In this recipe, we will only implement the RetrieveCustomerByCriteria operation.

Getting ready

Import the SoapUI project CustomerServiceCRM-soapui-project.xml from the location \chapter-5\getting-ready\exposing-restful-service\soapui into your SoapUI. Start the mock service CustomerServiceSOAP MockService.

Import the OSB project containing the implementation of the RESTful service into Eclipse OEPE from \chapter-5\solution\exposing-restful-service.

Import the base OSB project containing the right folder structure and the necessary XQuery transformations into Eclipse from \chapter-5\getting-ready\consuming-restful-service.

How to do it...

We will start with the business service, which will wrap the RESTful service. In Eclipse OEPE, perform the following steps:

  1. In the business folder of the consuming-restful-service project, create a new business service and name it CustomerService.
  2. On the General tab select Messaging Service for the Service Type.
  3. Navigate to the Messaging tab and select XML for both the Request Message Type and the Response Message Type.
  4. Navigate to the Transport tab, enter http://localhost:7001/exposing-restful-service/CustomerService into the Endpoint URI field and click Add.

    The business service wrapping the RESTful service is in place. Let's now create the proxy service that will expose the SOAP-based interface. In Eclipse OEPE, perform the following steps:

  5. In the proxy folder create a new proxy service and name it CustomerService.
  6. On the General tab select WSDL Web Service for the Service Type and click Browse.
  7. Navigate to the CustomerServiceCRM.wsdl from the wsdl folder and select the CustomerServiceSOAP (port).
  8. Click OK and confirm the pop-up window with Yes.
  9. Navigate to the Message Flow tab, insert an Operational Branch node and name it OperationalBranch.
  10. Click on the first branch and select RetrieveCustomerByCriteria in the Operation drop-down listbox.
  11. Insert a Route node into the RetrieveCustomerByCriteria branch and name it InvokeGetRoute.
  12. Insert a Routing action into the Route node.
  13. On the Properties tab of the Routing action, click Browse, select the CustomerService business service in the business folder and click OK.
  14. Insert a Transport Header action into the Request Action flow of the Routing action.
  15. On the Properties tab of the Transport Header action, enable the Pass all Headers option.
  16. Insert an Insert action after the Transport Header action.
  17. On the Properties tab of the Insert, click <Expression> and enter <http:http-method>GET</http:http-method> into the Expression field. Click OK.
  18. Click on <XPath>, enter ./ctx:transport/ctx:request into the Expression field and click OK.
  19. Enter outbound into the In Variable field.

  20. Add another Insert action right after the one created above in step 16.
  21. On the Properties tab of the Insert, click <Expression> and navigate to the XQuery Resources tab.
  22. Click Browse, select the SoapToHttpGet.xq resource in the transformationfolder and click OK.
  23. In the Variable Structures on the right, expand body | $body – RetrieveCustomerByCriteria (request) and drag the node RetrieveCustomerByCriteria to the Binding of the retrieveCustomerByCriteria1 variable.

  24. Click on <XPath>, enter ./ctx:transport/ctx:request into the Expression field and click OK.
  25. Enter outbound into the In Variable field.

  26. Insert a Replace action into the Response Action flow.
  27. On the Properties tab of the Replace action, enter body into the In Variable field.
  28. Click <Expression>, navigate to the XQuery Resources tab and click Browse.
  29. Select the HttpGetToSoap.xq resource in the transformation folder and click OK.
  30. Enter $body/cus1:Customer into the Binding field of the customer1 variable.
  31. Add a new namespace and enter cus1 into the prefixhttp://www.somecorp.com/customer into the URI field and click OK.
  32. Select the Replace node contents option.

    By doing that the processing for the RetrieveCustomerByCriteria operation is complete. We won't implement the other operations of the Operational Branch in this recipe. It's a good practice to raise an error if a not-yet-supported operation is called. We can do this by adding a Raise Error action in the default branch:

  33. Insert a Pipeline Pair node into the Default branch and name it UnsupportedOpPipeline.
  34. Insert a Stage node into the Pipeline Pair and name it RaiseErrorStage.
  35. Insert a Raise Error action into the RaiseErrorStage.
  36. On the Properties tab of the Raise Error action, enter OPERATION_NOT_SUPPORTED into the Code field and The operation is not yet supported! into the Message field.

  37. Deploy the project to the OSB server.

    Our SOAP-based web service is now ready and can be tested. In the Service Bus console, perform the following steps:

  38. In the Project Explorer navigate to the CustomerService proxy service inside the proxy folder and click on the Launch Test Console icon.
  39. In the Available Operations drop-down listbox make sure to select the RetrieveCustomerByCriteria operation.
  40. In the Payload field, enter id into the criteriaField and 100 into the criteriaValue element and click Execute.
    How to do it...
  41. The Response Document in the next window will show the result of our SOAP-based web service.

How it works...

In this recipe, we created a SOAP-based web service for an existing RESTful service. We have mapped the SOAP request into the corresponding RESTful message and vice versa.

On the business service, wrapping the RESTful service, the HTTP method is defined at development time (POST by default). However, through the use of the Transport Header action together with the two Insert actions, we are able to overwrite/define the HTTP method being used in a given call. In our case, where we only implemented the RetrieveCustomerByCriteria, it's the HTTP GET method we want to use.

The next screenshot shows the value of the metadata in the $outbound variable in the Routing action.


We can see that the value of the http-method and the query-parameters elements that have been added by the two Insert actions.

For the creation of the query-parameters in the XQuery script, we have reused the standard OSB XML Schema HttpTransport.xsd. In there the query-parameters structure is defined as a complex type.

猜你喜欢

转载自blog.csdn.net/zy_27_ok/article/details/80326687