DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
  • Building a Containerized Quarkus API on AWS ECS/Fargate With CDK
  • Implementing Budget Policies and Budget Limits on Databricks
  • Unlocking the Benefits of a Private API in AWS API Gateway

Trending

  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  • The Third Culture: Blending Teams With Different Management Models
  • DevOps Is Dead, Long Live Platform Engineering
  1. DZone
  2. Data Engineering
  3. Databases
  4. Our Tryst With AWS API Gateway and XML Transformation

Our Tryst With AWS API Gateway and XML Transformation

See a solution to the issue of not being able to transform XML to JSON with AWS API Gateway.

By 
Arvind singh user avatar
Arvind singh
·
Sep. 11, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
24.9K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

AWS API Gateway

Unfortunately, the world has not yet upgraded itself to JSON, and a considerable amount of the world still uses XML-based SOAP services.

You might also enjoy:  Payload Transformation: JSON to XML

Background

Our tryst with XML and AWS API Gateway began when our client wanted to replace a Java-based middle layer application, which was proxying legacy SOAP services with AWS API Gateway. The middle layer application was calling the SOAP services using Apache axis2 client. Our use case looked like this:

  • Consumers will communicate with Gateway using only JSON.
  • Gateway will communicate with legacy backend SOAP services only using application/XML.

The Problem

At first, everything looked fine. We quickly integrated the backend SOAP service from Gateway. But then, we realized some great limitations of AWS API GW for our use case:

  • It does not support XML to JSON transformation.
  • It does not parse XML content to modify it, though velocity scripting language provides a great deal of flexibility to modifying JSON.

With the limitations provided, all the great capabilities of AWS API Gateway were not enough for us.

Possible Solutions

  1. Use Lambda — Call SOAP service and data transformation in lambda and integrate it into GW. We found quite a few blogs suggesting using Lambda for this kind of transformation. Though lambdas are great, in our use case, it meant:

    • Write code for data transformation for 100+ backend SOAP services. Maintaining JSON to SOAP request transformation for every service at Lambda meant more code upkeep and not leveraging the power of GW.

    • Also, AWS API GW does not support sending XML in raw format to lambda. XML can be put into a JSON by escaping all spaces and double quotes making JSON difficult to manage and understand.

With the approach looking difficult, we landed on the below solution.

Actual Solution

Beanstalk to the Rescue

We created a generic POST type REST service that accepts application/XML and:

  • Calls a SOAP service using Spring Boot and Apache HTTP client.
  • Converts the response SOAP response from the backend service to JSON, using Jackson fasterxml.

We deployed this service on a beanstalk. Luckily, we had other applications on beanstalk, and adding another service was not a roadblock for us.

@PostMapping(path="generic/rest")
       public ResponseEntity<?> getBusinessValue(@RequestBody String soapRequest, @PathVariable("service") String service) throws ClientProtocolException, IOException {
              HttpClient client = HttpClients.custom().build();
              HttpEntity httpEntity = new StringEntity(soapRequest);
              HttpPost postRequest = new HttpPost(service_path);
              postRequest.setEntity(httpEntity);
              HttpResponse httpResponse = client.execute(postRequest);
              HttpEntity responseEntity = httpResponse.getEntity();
              String entiryString = EntityUtils.toString(responseEntity);
              JsonNode response = fetchFromSoapResponse(entiryString);
           return new ResponseEntity<>(response, responseHeaders, HttpStatus.OK);
       }

       public static JsonNode fetchFromSoapResponse(String xmlString) throws IOException {
              XmlMapper xmlMapper = new XmlMapper();
              JsonNode node = xmlMapper.readTree(xmlString.getBytes());
              return node;
       }

Our solution looked like:

Beanstalk solution

GW accepts the JSON, and using the mapping template transforms the JSON into XML. GW integrates the above-mentioned REST Service deployed on beanstalk with the transformed XML as payload. This beanstalk does the magic of calling the backend SOAP service and converts the SOAP response to JSON. GW receives this JSON and passes it back to the consumers.

The following is a sample model:

The following is the sample mapping transformation of XML, which is sent to a POST type REST service deployed on beanstalk:

The following response is returned by GW:

Conclusion

The solution we used is no silver bullet, but given the limitations provided by AWS API Gateway, this is what we could offer.

Further Reading

API Gateway to the Rescue

XML API AWS

Opinions expressed by DZone contributors are their own.

Related

  • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
  • Building a Containerized Quarkus API on AWS ECS/Fargate With CDK
  • Implementing Budget Policies and Budget Limits on Databricks
  • Unlocking the Benefits of a Private API in AWS API Gateway

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook