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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Choosing a Library to Build a REST API in Java
  • Composite Requests in Salesforce Are a Great Idea
  • Creating a Secure REST API in Node.js

Trending

  • Rust: The Must-Adopt Language for Modern Software Development
  • Parallel Data Conflict Resolution in Enterprise Workflows: Pessimistic vs. Optimistic Locking at Scale
  • *You* Can Shape Trend Reports: Join DZone's Data Engineering Research
  • Docker Model Runner: Running AI Models Locally Made Simple
  1. DZone
  2. Data Engineering
  3. Databases
  4. Build a REST API with XML Payload

Build a REST API with XML Payload

Creating REST APIs with XML payloads.

By 
Neerav Aggarwal user avatar
Neerav Aggarwal
·
Apr. 23, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
47.9K Views

Join the DZone community and get the full member experience.

Join For Free

There is hardly any argument on the fact that APIs are increasingly becoming an important part of how companies do business. API has become the de facto standard to unlock one of their most valuable resources, data.

And as organizations are publishing more APIs, the observed trend is that REST is replacing SOAP as the data transfer protocol of choice. When it comes to the data that APIs serve up, XML is still the most used format. Although JSON is hot on its heels, we still need to address plenty of use cases with XML payload.

In this post, we will go step by step through an example to build out a RESTful web service with XML request and response payload. Check out the complete project on Anypoint Exchange.

Pre-requisites:

  • Sign up for an Anypoint Platform Account.
  • Download Anypoint Platform Design Tool – MuleSoft Anypoint Studio. In this post, we are using version 5.4 of Anypoint Studio with the new “Studio Light Theme”. You can always revert to the old theme in Preferences.
  • A database where we can create and load a sample table required for this project. In this example, we will use an instance of the MySQL database.
  • To follow the example in this guide, run the SQL script customer.sql to set up the table and load it with some sample data.

Steps:

  1. Since we are working with an XML payload, we will either have an existing schema for the request and response message or create one from scratch. In this example, we will consider the latter. Since the foundation for Anypoint Studio is the Eclipse IDE, we can leverage the XML schema design tool which comes included with Anypoint Studio.
    Open Anypoint Studio and create a “General Project”.
    Fullscreen_12_25_15__4_17_PM
  2. Create a new file in the project of type “XML Schema File”. Refer link to get step by step instructions in designing the XML Schema using the Eclipse-based schema editor. For the scope of this example, download the schema for request and response XML payload.
    New_and_Mule_Design_
    Now that we have the schema definitions ready we can start defining the API specification. We do this with the help of RESTful API Modeling Language (RAML). RAML is central to the design first approach to implementing an API that we have discussed in out previous blog posts(link here).
  3. To start defining an API specification, first, sign up for an Anypoint account if you don’t already have one.
    Anypoint_Platform_and_Anypoint_Platform
  4. After logging into the account click on “API” on the top which will bring up the page listing any existing APIs. If this is the first API, then the list will be blank.
    Anypoint_Platform_and_Postman_and_Mule_Design_-_restxmlpayload_src_main_app_api_xml_-_Anypoint_Studio_-__Users_neerav_agrawal_Mule_Workspace_MuleWorkspace_Workspace_NewStudio
  5. Click on “Add new API” which brings up the API detail page where on the left side we click on “Define API in API Designer.” which brings up the API Designer tool.
    Mule_Workspace_MuleWorkspace_Workspace_NewStudio
  6. Using the API Designer tool where we will start creating our API specification using RAML. Import the schema files we created earlier into API designer by clicking on “Import”.
    ImportSchema
  7. Using simple descriptive language, we will document the requirements for our API to query customer details. For more information, please refer to the RAML tutorial here.
    For this example download the RAML spec here.
    ramlSpecCustomer
  8. Once we have confirmed and documented the API requirements, we will export the resulting RAML file along with the referenced XSD.
    Using the API specification we just exported we will now configure the actual implementation of the operations defined in the RAML file. For this purpose, we will use Mulesoft Anypoint Studio.
    exportRaml
  9. Open up Studio. Create a new “Mule Project”. Give a name to the project. Since we are implementing the operations defined in the RAML file, we will reference the exported RAML archive at the bottom. Click Finish.
    createProjectRAML1
  10. Referencing the RAML file will preconfigure the application with the flows and exception handling.
    We will configure the “Post” operation and add components to query the database and map the result set to the required XML payload.
    AddToPost
  11. Add the database connector. In this example, we are using a MYSQL database. So configure the connector with MySQLvers. You can configure any other JDBC database drivers here. Add in the relevant connection parameters and click “Finish”.Screenshot_1_8_16__5_00_PM
  12. Pick the “Select” operation and configure the select query. In this example, we are fetching the customerID from the XML input request. We will use this customerID in the select query to fetch the relevant customer record from the database table. We use an XPATH3 function to parse through the incoming XML payload. We use wildcard “*” to escape the namespace. If there is a need to incorporate the namespace, then refer to the documentation.
    dbSelectQuery
  13. Now that we have configured the database connector, we need to map the result set returned from the connector to the XML response payload. For this purpose, we will use the DataWeave transform component. Because of DataSense, metadata on the left and right side are automatically fetched making it very easy to map the data.
    transformCustomerOutput
  14. Using the mapper, drag and drop the result set from left to right side. After which we map each relevant data element from left to right side.
    transformMapping_123
  15. Right-click on the project and run it in Anypoint Studio as a mule application which will open up the API console in Studio. We can test the API here by adding a sample request XML with a customer id. Press Send, which executes the flow we just configured and to fetch the customer details from the database table.
    runApplication

You can download this complete project from Anypoint Exchange.

In this post and from our previous post we have understood how easy it is to create an API with the end user in mind.

The trends suggest the shift towards RESTful APIs but the decision of the data format between JSON or XML is still fifty-fifty. In any case, we can have either or both data formats in our API repository.

In the next post, we will see how we can configure composite processes by utilizing multiple APIs with both JSON and XML data formats.

API XML REST Web Protocols Payload (computing) Database Build (game engine)

Published at DZone with permission of Neerav Aggarwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Choosing a Library to Build a REST API in Java
  • Composite Requests in Salesforce Are a Great Idea
  • Creating a Secure REST API in Node.js

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: