Clean • Professional
When building REST APIs with Spring Boot, understanding data formats is essential. Data formats define how information is structured when sent between a client (like a browser, mobile app, or another server) and your API. The right format ensures seamless communication, compatibility, and efficient data processing.
Spring Boot automatically handles multiple data formats using its HttpMessageConverter system, making it easy for developers to send and receive data in the desired structure.
A data format specifies how data is structured for requests and responses in APIs:
A data format defines how data is structured for exchange between client and server, ensuring proper understanding and processing.
APIs exchange data between clients (like browsers, mobile apps) and servers. The data format defines how this information is structured, so both sides can interpret it correctly. Spring Boot supports multiple formats via HttpMessageConverter.
Example – Request Body (POST /users):
{
"name":"Alice",
"email":"[email protected]",
"age":25
}
Example – Response Body (GET /users/1):
{
"id":1,
"name":"Alice",
"email":"[email protected]",
"age":25
}
Usage :
@RequestBody and returns Java objects as JSON using @ResponseBody or ResponseEntity.Example – Request Body (POST /users):
<User>
<name>Alice</name>
<email>[email protected]</email>
<age>25</age>
</User>
Example – Response Body (GET /users/1):
<User>
<id>1</id>
<name>Alice</name>
<email>[email protected]</email>
<age>25</age>
</User>
Usage :
@RequestBody and convert Java objects to XML responses using @ResponseBody or ResponseEntity.@ModelAttribute or @RequestParam to map form parameters to Java objects.Example – Request Body (Form Data):
name=Alice&[email protected]&age=25
Example – Controller in Spring Boot:
@PostMapping("/submitForm")
public ResponseEntity<String>submitForm(@ModelAttribute UserForm userForm) {
// Access form data via userForm object
return ResponseEntity.ok("Form submitted successfully");
}
Usage :
Content-Type header must be set as:Content-Type: application/x-www-form-urlencoded
@RequestParam MultipartFile or multiple files with MultipartFile[].Example – File Upload Controller:
@PostMapping("/upload")
public ResponseEntity<String>uploadFile(@RequestParam("file") MultipartFile file) {
// Process the uploaded file
return ResponseEntity.ok("File uploaded successfully");
}
Example Request (Multipart Form Data):
--boundary
Content-Disposition: form-data;name="name"
Alice
--boundary
Content-Disposition: form-data;name="file"; filename="profile.jpg"
Content-Type: image/jpeg
(binary data here)
--boundary--
Usage :
@RequestParam maps each part to the corresponding method parameter.@RequestParam("files") MultipartFile[] files
Content-Type header must be:Content-Type: multipart/form-data
YAML (YAML Ain't Markup Language):
Example (YAML Request Body):
name:Alice
<email:[email protected]>
age:25
Binary Formats: High-performance formats used for efficient communication, especially in microservices.
Usage :
HttpMessageConverters.Spring Boot automatically converts incoming requests and outgoing responses between Java objects and different data formats using HttpMessageConverters. Depending on the data format, you can handle them in different ways.
| Data Format | How to Handle in Spring Boot | Notes |
|---|---|---|
| JSON | Use @RequestBody for requests and @ResponseBody for responses. Jackson handles serialization and deserialization automatically. | Default format for REST APIs; most widely used. |
| XML | Enable Jackson XML or JAXB dependency. Use @RequestBody and @ResponseBody. | Suitable for enterprise or legacy systems that require strict XML schemas. |
| Form Data (application/x-www-form-urlencoded) | Use @ModelAttribute or @RequestParam to bind request parameters. | Common in HTML forms or simple POST requests. |
| Multipart/Form-Data (multipart/form-data) | Use @RequestParam MultipartFile file for file uploads. | Supports uploading files or sending mixed text and binary data. |
Proper headers are essential for the server and client to understand the data format:
application/json → JSONapplication/xml → XMLmultipart/form-data → File uploadAccept: application/json → JSONAccept: application/xml → XML| Feature | JSON (JavaScript Object Notation) | XML (Extensible Markup Language) |
|---|---|---|
| Format Type | Lightweight text-based format | Text-based markup language |
| Readability | Easy to read and write; cleaner syntax | Verbose and less human-readable due to tags |
| Data Structure | Uses objects ({}) and arrays ([]) | Uses nested tags <tag></tag> |
| Size / Performance | Smaller payload; faster parsing | Larger payload; slower parsing due to verbose tags |
| Support in Spring Boot | Default format; handled by Jackson automatically | Requires Jackson XML or JAXB dependencies |
| Use Case | Web and mobile apps, REST APIs | Legacy systems, SOAP APIs, enterprise applications |
| Schema / Validation | JSON Schema (optional) | XSD (XML Schema Definition) is common and stricter |
| Data Types | Supports strings, numbers, booleans, arrays, objects | All data is text; additional parsing needed for numbers, booleans |
| Ease of Parsing | Simple, widely supported by languages | More complex due to nested tags and attributes |
| Support for Attributes | No concept of attributes; only key-value pairs | Supports attributes in tags <user id="1">Alice</user> |
| Comments | Limited support (only in JSON5 or other variants) | Fully supports comments <!-- Comment --> |
Understanding data formats is crucial for creating robust, flexible, and developer-friendly Spring Boot APIs. Choosing the right format ensures that: