Expose a web service as a REST API

Overview

You can import a WSDL file into Policy Studio, and invoke it from a policy instead of exposing it to a client.

For example, in a SOAP to REST use case, the web service is registered in Policy Studio by importing a WSDL file into the web service repository. A REST API is then defined in Policy Studio, which calls a policy to implement the API, and in turn, this policy invokes the web service.

Summary of steps

The steps involved in exposing a SOAP web service as a REST API are summarized as follows:

  1. Virtualize a SOAP web service
  2. Define a REST API
  3. Route REST requests through the virtualized SOAP service
  4. Test the REST to SOAP mapping

Virtualize a SOAP web service

To expose a virtualized version of a SOAP web service on API Gateway, import a WSDL file describing the web service into the web service repository.

The following figure shows importing the WSDL for a currency conversion SOAP web service.

Note   This is an example for demonstration purposes only. Adapt the following steps to suit your web service.

Import WSDL for SOAP web service

For more information on using the Import WSDL wizard, see Import a WSDL file.

When you register a web service in Policy Studio, service handlers and policies are autogenerated.

The following figure shows the generated policies for the currency conversion service.

Generated policies for web service

Define a REST API

The next step is to define a REST API for a currency conversion service as follows:

  1. Create a new policy container called CurrencyConversion.
  2. Within this policy container create a new policy called MainRouter.
  3. Add a Policy Shortcut filter to the MainRouter policy, and set it to call the CurrencyConvertor policy that was autogenerated when you imported the WSDL for the web service.
  1. Add a Validate REST filter to the MainRouter policy. Set the Method to GET, add two request parameters called fromCurrency and toCurrency, and select the Extract valid parameters into individual message attributes check box:
  2. Validate REST filter
  3. Add a relative path for the MainRouter policy so that all REST requests received by API Gateway on the path /convertcurrency/getConversionRate are processed by the MainRouter policy. On the HTTP Method tab of the path resolver, set the method to GET.
  4. Add relative path
  5. At this stage, the MainRouter policy should look like this:
  6. MainRouter policy (1)

Route REST requests through the virtualized SOAP service

To route REST requests through the virtualized SOAP service, perform the following sequence of tasks:

  1. Create a request processing policy
  2. Create a response processing policy

Create a request processing policy

First, create a dedicated request processing policy to create the SOAP request message body to send to the SOAP service:

  1. Create a request processing policy called GetCurrencyConvertorRequest.
  2. Add a Set HTTP verb filter to the policy and enter POST in the HTTP Verb field.
  3. Add an Add HTTP header filter to the policy with the following settings:
  4. Add HTTP header
  5. Add a Set Message filter to the policy and enter text/xml as the Content-Type.
  6. Select From web service operation from the Populate menu and select the ConversionRate operation from the currency conversion web service. This populates the contents of the message body.
  7. Set message
  8. You need to replace the currencies in the message body with the currencies from the incoming REST request. To enable the autocompletion mechanism in the Set Message filter, which will allow you to insert the fromCurrency and toCurrency selector strings in the message body, you must first connect up the filters in the MainRouter and GetCurrencyConvertorRequest policies.
  9. Add a Policy Shortcut filter to the MainRouter policy, and set it to call the GetCurrencyConvertorRequest policy.
  10. Connect the filters in the MainRouter policy as follows:
  11. MainRouter policy (2)
  12. Connect the filters in the GetCurrencyConvertorRequest policy as follows:
  13. GetCurrencyConvertorRequest policy
  14. Edit the Set Message filter. Select the currency type in the message body and start typing to see matching selectors.
  15. Set message autocompletion
  16. Insert the selectors $params.query.fromCurrency and $params.query.toCurrency in place of the currency types.
  17. Set message with selectors

Create a response processing policy

Next, create a response processing policy to convert the XML returned from the SOAP web service to JSON format:

  1. Create a response processing policy called GetCurrencyConvertorResponse.
  2. Add an XML to JSON filter to the policy. Configure it to extract the SOAP Body content first and remove any namespaces:
  3. XML to JSON filter
  4. The GetCurrencyConvertorResponse policy should now look like this:
  5. GetCurrencyConvertorResponse policy
  6. Add another Policy Shortcut filter to the MainRouter policy, and set it to call the GetCurrencyConvertorResponse policy.
  7. At this stage, the MainRouter policy should look like this:
  8. Complete MainRouter policy

Test the REST to SOAP mapping

To test the REST to SOAP mapping, deploy the configuration on the API Gateway and send a REST request from a REST client. For example, to get the conversion rate for EUR to USD, send a request to the URL:

http://localhost:8080/convertcurrency/getConversionRate?fromCurrency=EUR&toCurrency=USD

The following is an example JSON response:

{
    "ConversionRateResponse": {
        "ConversionRateResult": 1.1194
    }
}

You can use API Gateway Manager to view the filter execution path for the REST request, and to examine the request from the client and the response from API Gateway, and the request from API Gateway and the response from the web service.

Execution path in API Gateway Manager

For more information on API Gateway Manager, see the API Gateway Administrator Guide.

Related Links