REST JSON to SOAP conversion tutorial
Join the DZone community and get the full member experience.
Join For Freei often get asked about ‘rest to soap’ transformation use cases these days. using an soa gateway like securespan to perform this type of transformation at runtime is trivial to setup. with securespan in front of any existing web service (in the dmz for example), you can virtualize a rest version of this same service. using an example, here is a description of the steps to perform this conversion.
imagine the geoloc web service for recording geographical locations. it has two methods, one for setting a location and one for getting a location. see below what this would look like in soap.
request:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<geo:getposition xmlns:geo="http://test.layer7tech.com/geolocws">
<geo:trackerid>34802398402</geo:trackerid>
</geo:getposition>
</soapenv:body>
</soapenv:envelope>
response:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<geo:getpositionres xmlns:geo="http://test.layer7tech.com/geolocws">
<geo:latitude>52.37706</geo:latitude>
<geo:longitude>4.889721</geo:longitude>
</geo:getpositionres>
</soapenv:body>
</soapenv:envelope>
request:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<geo:setposition xmlns:geo="http://test.layer7tech.com/geolocws">
<geo:trackerid>34802398402</geo:trackerid>
<geo:latitude>52.37706</geo:latitude>
<geo:longitude>4.889721</geo:longitude>
</geo:setposition>
</soapenv:body>
</soapenv:envelope>
response:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<geo:setpositionres xmlns:geo="http://test.layer7tech.com/geolocws">ok</geo:setpositionres>
</soapenv:body>
</soapenv:envelope>
here is the equivalent rest target that i want to support at the edge. payloads could be xml, but let’s use json to make it more interesting.
get /position/34802398402
http 200 ok
content-type: text/json
{
'latitude' : 52.37706
'longitude' : 4.889721
}
post /position/34802398402
content-type: text/json
{
'latitude' : 52.37706
'longitude' : 4.889721
}
http 200 ok
ok
now let’s implement this rest version of the service using securespan. i’m assuming that you already have a securespan gateway deployed between the potential rest requesters and the existing soap web service.
first, i will create a new service endpoint on the gateway for this service and assign anything that comes at the uri pattern /position/* to this service. i will also allow the http verbs get and post for this service.
next, let’s isolate the resource id from the uri and save this as a context variable named ‘trackerid’. we can use a simple regex assertion to accomplish this. also, i will branch on the incoming http verb using an or statement. i am just focusing on get and post for this example but you could add additional logic for other http verbs that you want to support for this rest service.
for get requests, the transformation is very simple, we just declare a message variable using a soap skeleton into which we refer to the trackerid variable.
this soap message is routed to the existing web service and the essential elements are isolated using xpath assertions.
the rest response is then constructed back using a template response.
a similar logic is performed for the post message. see below for the full policy logic.
you’re done for virtualizing the rest service. setting this up with securespan took less than an hour, did not require any change on the existing soap web service and did not require the deployment of an additional component. from there, you would probably enrich the policy to perform some json schema validation , some url and query parameter validation, perhaps some authentication, authorization , etc.
Published at DZone with permission of Francois Lascelles, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments