Expose a web service as a REST API

Overview

You can import a WSDL file into Policy Studio, and instead of exposing it to a client, invoke it from a policy. 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 the SOAP web service.

  2. Define a new REST API.

  3. Route all 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 stock quote SOAP web service exposed on the URL:

http://www.webservicex.net/stockquote.asmx?WSDL

Import WSDL for SOAP web service

For more information on using the Import WSDL wizard, see the section called “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 stock quote service.

Generated policies for web service

Define a REST API

The next step is to define a REST API for a stock quote service. You can develop REST APIs in Policy Studio using the REST API development wizard. To launch the wizard, right-click the Business Services > REST API Repository node in the Policy Studio tree and select Add REST API.

Follow these steps:

  1. Define a REST API called StockQuote and expose it on the base path /stockquote.

  2. Define a REST API method called GetStockQuote. Set the exposure settings as follows and add an inbound parameter for the stock symbol:

    GetStockQuote REST API method

  3. Set the routing policy to the StockQuote policy that was autogenerated when you imported the WSDL for the web service. For example:

    GetStockQuote routing policy

Route REST requests through the virtualized SOAP service

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

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 GetStockQuoteRequest.

  2. Add a Set Message filter to the policy.

    • Enter text/xml as the Content-Type.

    • Select From web service operation from the Populate menu and select the GetQuote operation from the stock quote web service. This populates the contents of the message body.

      Set message

    • To insert a REST API parameter for the stock symbol, right-click in the message body and select Insert > REST API parameter.

      Insert REST API parameter in message body

      Select the symbol parameter from the Insert REST API Parameter dialog.

      This replaces the selected text with the ${params.query.symbol} selector string:

      Selector string in message body

  3. Add a Set HTTP verb filter to the policy and enter POST in the HTTP Verb field.

  4. Add an Add HTTP header filter to the policy with the following settings:

    Add HTTP header

The following figure shows the completed policy.

Request processing policy

Set the request policy on the REST API method

Next, edit the REST API method GetStockQuote and set this policy as the request processing policy.

Create a response processing policy

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

  1. Create a response processing policy called XML To JSON.

  2. Add an XML to JSON filter to the policy. Configure it to extract the SOAP Body content first and remove any namespaces:

    XML to JSON filter

Set the response policy on the REST API method

Finally, edit the REST API method GetStockQuote and set this policy as the response processing 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 a stock quote for Amazon, send a request to the URL:

http://localhost:8080/stockquote/getStockQuote?symbol=AMZN

The following is an example JSON response:

{
    "GetQuoteResponse": {
        "GetQuoteResult": "<StockQuotes><Stock><Symbol>AMZN</Symbol>
        <Last>381.83</Last><Date>2/13/2015</Date><Time>4:00pm</Time>
        <Change>+4.66</Change><Open>378.41</Open><High>383.00</High>
        <Low>377.01</Low><Volume>3475069</Volume><MktCap>177.3B</MktCap>
        <PreviousClose>377.17</PreviousClose><PercentageChange>+1.24%</PercentageChange>
        <AnnRange>284.00 - 383.11</AnnRange><Earns>-0.522</Earns>
        <P-E>N/A</P-E><Name>Amazon.com</Name></Stock></StockQuotes>"
    }
}