The API Gateway has a default mediation flow for the API invocation requests that it receives. You can extend this default mediation flow to do additional custom mediation for the messages in the API Gateway. An extension is provided as a synapse mediation sequence. You can design sequences using a tool like WSO2 Developer Studio and then store the sequence in the Gateway's registry.
Let's see how to convert a message types using custom sequences. In this tutorial, we convert a JSON payload to SOAP before sending it to a SOAP backend. Then we receive the response in SOAP and convert it back to JSON.
This tutorial uses the WSO2 API Manager Tooling Plug-in and the
PhoneVerification
API created in Create and Publish an API.- Log in to the API Publisher and click the
PhoneVerification
API. - Click the Edit icon to go to its edit mode.
- Add the following resource to the API.Tip: The resource you create here invokes the SOAP 1.2 Web service of the backend. Therefore, the recommended method is HTTP POST. As you do not include the payload in a query string, avoid giving any specific name in the URL pattern, which will be amended to the actual backend URL.FieldSample value
Resources URL pattern /* Request types POST - After the resource is added, expand it and edit the parameter as follows. This parameter is used to pass the payload to the backend.Parameter nameDescriptionParameter TypeData TypeRequired
Payload Pass the phone number and license key body String true Next, let's write a sequence to convert the JSON payload to a SOAP request. We do this because the backend accepts SOAP requests. - Navigate to the Implement page and change the endpoint of the API to http://ws.cdyne.com/phoneverify/phoneverify.asmx?WSDL. Once the edits are done, click Save.
- Download and install the WSO2 API Manager Tooling Plug-in if you have not done so already. Open Eclipse by double clicking the
Eclipse.app
file inside the downloaded folder. - Click Window > Open Perspective > Other to open the Eclipse perspective selection window. Alternatively, click the Open Perspective icon shown below at the top right corner.
- On the dialog box that appears, click WSO2 APIManager and click OK.
- On the APIM perspective, click the Login icon as shown below.
- On the dialog box that appears, enter the URL, username and password of the Publisher server.
- On the tree view that appears, expand the folder structure of the existing API.
- Right-click on the
in
sequence folder and click Create to create a newin
sequence. - Name the sequence
JSONtoSOAP
. - Your sequence now appears on the Developer Studio console. From under the Mediators section, drag and drop a PayloadFactory mediator to your sequence and give the following values to the mediator.Tip: The PayloadFactory mediator transforms the content of your message. The
<args>
elements define arguments that retrieve values at runtime by evaluating the provided expression against the SOAP body. You can configure the format of the request/response and map it to the arguments.For example, in the following configuration, the values for the format parametersPhoneNumber
andLicenseKey
will be assigned with values that are taken from the<args>
elements (arguments,) in that particular order.For details on how you got this configuration, see PayloadFactory Mediator in the WSO2 ESB documentation.Payload Args Give the following arguments:TypeValueEvaluatorexpression //request/PhoneNumber
xml expression //request/LicenseKey
xml - Similarly, add a Property mediator to the same sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/soap+xml Property Scope axis2 - Save the sequence, which is in XML format (e.g.,
JSONtoSOAP.xml
). This will be theIn
sequence for your API. Next, create anout
sequence. - Name the sequence
SOAPtoJSON
. - Add a Property mediator to the sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/json Property Scope axis2 - Save the sequence, which is in XML format (e.g.,
SOAPtoJSON.xml
). This will be theOut
sequence for your API. - Click the Push all changes to the server icon shown below to commit your changes to the Publisher server.
- Log back in to the API Publisher, click the Edit link associated with the API and navigate to the Implement tab. Select the Enable Message Mediation check box and engage the
In
andout
sequences that you created earlier. - Save the API.
You have created an API, a resource to access the SOAP backend and engaged sequences to the request and response paths to convert the message format from JSON to SOAP and back to JSON. Let's subscribe to the API and invoke it. - Log in to the API Store and subscribe to the API and create an access token if you have not done so already.
- Go to the API Console tab and expand the POST method.
- Give the payload in the
body
parameter in JSON format and click Try it out. Here's a sample JSON payload: {"request":{"PhoneNumber":"18006785432","LicenseKey":"0"}} - Note that you get a JSON response to the JSON request whereas the backend accepts SOAP messages. The request and response are converted by the sequences that you engaged.
In this tutorial, you converted a message from JSON to SOAP and back to JSON using
In
and Out
sequences.
0 comentarios:
Publicar un comentario