|
| 1 | +--- |
| 2 | +id: java-spring-boot-xml |
| 3 | +title: Sample REST API with Spring-Boot and XML |
| 4 | +sidebar_label: XML API App |
| 5 | +description: This CRUD Application demonstrates REST API endpoints that serve XML responses using Spring Boot. |
| 6 | +tags: |
| 7 | + - java |
| 8 | + - spring-boot |
| 9 | + - xml |
| 10 | + - quickstart |
| 11 | + - samples |
| 12 | + - examples |
| 13 | + - tutorial |
| 14 | + - restful-api |
| 15 | +keyword: |
| 16 | + - XML |
| 17 | + - Maven |
| 18 | + - Springboot Framework |
| 19 | + - REST API |
| 20 | + - Java |
| 21 | + - API Test generator |
| 22 | + - Auto Testcase generation |
| 23 | + - JAXB |
| 24 | +--- |
| 25 | + |
| 26 | +## Introduction |
| 27 | + |
| 28 | +🌟 Explore creating REST APIs with XML responses using [Spring-Boot](https://github.com/spring-projects/spring-boot). Discover the ease of integrating XML serialization through JAXB. Let's dive right in! 🚀 |
| 29 | + |
| 30 | +## Pre-Requisite 🛠️ |
| 31 | + |
| 32 | +- Install [Java 17](https://www.oracle.com/java/technologies/downloads/) and set JAVA_HOME environment variable. |
| 33 | +- Install [Maven](https://maven.apache.org/install.html) |
| 34 | +- Install [Docker](https://docs.docker.com/engine/install/) (optional) |
| 35 | + |
| 36 | +## Get Started! 🎬 |
| 37 | + |
| 38 | +### Clone and Build the Application |
| 39 | + |
| 40 | +```bash |
| 41 | +git clone https://github.com/keploy/samples-java.git |
| 42 | +cd spring-boot-xml/naive-spring-boot |
| 43 | +mvn clean install |
| 44 | +``` |
| 45 | + |
| 46 | +### Run the Application |
| 47 | + |
| 48 | +Start the Spring Boot application with: |
| 49 | + |
| 50 | +```bash |
| 51 | +mvn spring-boot:run |
| 52 | +``` |
| 53 | + |
| 54 | +> **Note**: For Windows users, commands remain the same. |
| 55 | +
|
| 56 | +## API Endpoints 📡 |
| 57 | + |
| 58 | +### Get User Data (XML) |
| 59 | + |
| 60 | +- **Endpoint**: `GET /api/user` |
| 61 | + |
| 62 | +Make a request using `curl`: |
| 63 | + |
| 64 | +```bash |
| 65 | +curl -X GET -H \"Accept: application/xml\" http://localhost:8080/api/user |
| 66 | +``` |
| 67 | + |
| 68 | +- **Endpoint**: `GET /api/users` |
| 69 | + |
| 70 | +Make a request using `curl`: |
| 71 | + |
| 72 | +```bash |
| 73 | +curl -X GET http://localhost:8080/api/users -H "Accept: application/xml" |
| 74 | +``` |
| 75 | + |
| 76 | +- **Endpoint**: `GET /api/people` |
| 77 | + |
| 78 | +Make a request using `curl`: |
| 79 | + |
| 80 | +```bash |
| 81 | +curl -X GET http://localhost:8080/api/people -H "Accept: application/xml" |
| 82 | +``` |
| 83 | + |
| 84 | +### Example XML Response |
| 85 | + |
| 86 | +```xml |
| 87 | +<User> |
| 88 | + <name>John Doe</name> |
| 89 | + <age>30</age> |
| 90 | + <phone>0101233333</phone> |
| 91 | +</User> |
| 92 | +``` |
| 93 | + |
| 94 | +## Integrating with Keploy 📥 |
| 95 | + |
| 96 | +Harness Keploy's test generation capabilities easily: |
| 97 | + |
| 98 | +### Install Keploy |
| 99 | + |
| 100 | +```bash |
| 101 | + curl --silent -O -L https://keploy.io/install.sh && source install.sh |
| 102 | +``` |
| 103 | + |
| 104 | +or |
| 105 | + |
| 106 | +### Install the cloud version - |
| 107 | + |
| 108 | +```bash |
| 109 | + curl --silent -O -L https://keploy.io/ent/install.sh && source install.sh |
| 110 | +``` |
| 111 | + |
| 112 | +### Record Test Cases |
| 113 | + |
| 114 | +```bash |
| 115 | +keploy record -c "java -jar target/XML-0.0.1-SNAPSHOT.jar" |
| 116 | +``` |
| 117 | + |
| 118 | +Start recording interactions by running: |
| 119 | + |
| 120 | +```bash |
| 121 | +keploy record -c "java -jar target/XML-0.0.1-SNAPSHOT.jar" |
| 122 | +``` |
| 123 | + |
| 124 | +Then, invoke the API using `curl` or your favorite API testing tool. |
| 125 | + |
| 126 | +### Run Generated Tests |
| 127 | + |
| 128 | +Execute recorded tests: |
| 129 | + |
| 130 | +```bash |
| 131 | +keploy test -c "java -jar target/XML-0.0.1-SNAPSHOT.jar" --delay 10 |
| 132 | +``` |
| 133 | + |
| 134 | +Review generated test reports in `Keploy/reports`. |
| 135 | + |
| 136 | +## Handling Failing Tests ⚠️ |
| 137 | + |
| 138 | +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`: |
| 139 | + |
| 140 | +**Example:** |
| 141 | + |
| 142 | +<img width="694" alt="Screenshot 2025-03-11 at 12 07 04 AM" src="https://github.com/user-attachments/assets/92dc6480-73f9-435c-a3b8-c918b2acc7a1" /> |
| 143 | + |
| 144 | +```yaml |
| 145 | +globalNoise: |
| 146 | + global: |
| 147 | + header.Date: [] |
| 148 | + body: |
| 149 | + # To ignore some values for a field, pass regex patterns to the corresponding array value |
| 150 | + UserList: [] |
| 151 | +``` |
| 152 | +
|
| 153 | +After updating `keploy.yml` with the above configuration, rerun your tests, and the issue should be resolved. |
| 154 | + |
| 155 | +<img width="711" alt="Screenshot 2025-03-11 at 12 07 19 AM" src="https://github.com/user-attachments/assets/bed57c1e-e7a9-4cbd-80d6-f69a2024ba60" /> |
| 156 | + |
| 157 | +## Dependencies 📚 |
| 158 | + |
| 159 | +- Spring Boot |
| 160 | +- Spring Web |
| 161 | +- JAXB (XML serialization) |
| 162 | + |
| 163 | +## Wrapping Up 🎉 |
| 164 | + |
| 165 | +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! |
| 166 | + |
| 167 | +import GetSupport from '../concepts/support.md' |
| 168 | + |
| 169 | +<GetSupport/> |
0 commit comments