Expose a web service as a REST API
4 minute read
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.
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.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.
Define a REST API
The next step is to define a REST API for a currency conversion service as follows:
-
Create a new policy container called
CurrencyConversion
. -
Within this policy container create a new policy called
MainRouter
. -
Add a Policy Shortcut filter to the
MainRouter
policy, and set it to call theCurrencyConvertor
policy that was autogenerated when you imported the WSDL for the web service. -
Add a Validate REST filter to the
MainRouter
policy. Set the Method toGET
, add two request parameters calledfromCurrency
andtoCurrency
, and select the Extract valid parameters into individual message attributes check box: -
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 theMainRouter
policy. On the HTTP Method tab of the path resolver, set the method toGET
.At this stage, the MainRouter policy should look like this:
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:
-
Create a request processing policy called
GetCurrencyConvertorRequest
. -
Add a Set HTTP verb filter to the policy and enter
POST
in the HTTP Verb field. -
Add an Add HTTP header filter to the policy with the following settings:
-
Add a Set Message filter to the policy and enter
text/xml
as the Content-Type.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.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
andtoCurrency
selector strings in the message body, you must first connect up the filters in theMainRouter
andGetCurrencyConvertorRequest
policies. -
Add a Policy Shortcut filter to the
MainRouter
policy, and set it to call theGetCurrencyConvertorRequest
policy. -
Connect the filters in the
MainRouter
policy as follows: -
Connect the filters in the
GetCurrencyConvertorRequest
policy as follows: -
Edit the Set Message filter. Select the currency type in the message body and start typing to see matching selectors.
-
Insert the selectors
$params.query.fromCurrency
and$params.query.toCurrency
in place of the currency types.
Create a response processing policy
Next, create a response processing policy to convert the XML returned from the SOAP web service to JSON format:
-
Create a response processing policy called
GetCurrencyConvertorResponse
. -
Add an XML to JSON filter to the policy. Configure it to extract the SOAP Body content first and remove any namespaces:
-
The GetCurrencyConvertorResponse policy should now look like this:
-
Add another Policy Shortcut filter to the
MainRouter
policy, and set it to call theGetCurrencyConvertorResponse
policy.At this stage, the MainRouter policy should look like this:
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.