Skip to main content

Sample REST API with Spring-Boot and XML​

OSSEnterprise

Explore creating REST APIs with XML responses using Spring-Boot. Discover the ease of integrating XML serialization through JAXB. Let's dive right in!

Don’t have Keploy installed yet?

Before running this sample, make sure Keploy is installed on your system.

πŸ‘‰ Go to Installation Guide

Pre-Requisite πŸ› οΈβ€‹

  • Install Java 17 and set JAVA_HOME environment variable.
  • Install Maven
  • Install Docker (optional)

Get Started! πŸŽ¬β€‹

Clone and Build the Application​

git clone https://github.com/keploy/samples-java.git
cd spring-boot-xml/naive-spring-boot
mvn clean install

Run the Application​

Start the Spring Boot application with:

mvn spring-boot:run

Record Test Cases​

keploy record -c "java -jar target/XML-0.0.1-SNAPSHOT.jar"

Then, invoke the API using curl or your favorite API testing tool.

API Endpoints πŸ“‘β€‹

Get User Data (XML)​

  • Endpoint: GET /api/user

Make a request using curl:

curl -X GET -H \"Accept: application/xml\" http://localhost:8080/api/user
  • Endpoint: GET /api/users

Make a request using curl:

curl -X GET http://localhost:8080/api/users -H "Accept: application/xml"
  • Endpoint: GET /api/people

Make a request using curl:

curl -X GET http://localhost:8080/api/people -H "Accept: application/xml"

Example XML Response​

<User>
<name>John Doe</name>
<age>30</age>
<phone>0101233333</phone>
</User>

Run Generated Tests​

Execute recorded tests:

keploy test -c "java -jar target/XML-0.0.1-SNAPSHOT.jar" --delay 10

Review generated test reports in Keploy/reports.

Handling Failing Tests βš οΈβ€‹

If you encounter failing tests due to variable or irrelevant data, like timestamps or dynamically generated fields, you can add them to the global noise configuration in your keploy.yml:

Example:

Sample Keploy test header
globalNoise:
global:
header.Date: []
body:
# To ignore some values for a field, pass regex patterns to the corresponding array value
UserList: []

After updating keploy.yml with the above configuration, rerun your tests, and the issue should be resolved.

Sample Keploy Testcases

Wrapping Up πŸŽ‰β€‹

Fantastic! You've successfully navigated creating and testing XML APIs with Spring Boot and Keploy. Keep exploring, experimenting, and innovating! If you have any queries, we're here to help!