Зміст курсу
Spring Boot Backend
Spring Boot Backend
Data formats: JSON and XML
In the previous chapters, we explored how HTTP works and how it transfers data. In this chapter, we will look into formats such as JSON and XML, which are formats that can be used to transmit data in the body of a server response or when sending a request to the server.
What is JSON?
JSON is used for transmitting data in a key-value format, which makes it very convenient to access data and convert it directly into an object.
This format supports arrays, objects, as well as primitive values like strings, numbers, and boolean values.
Data Formats in JSON
For arrays in JSON, we can use the following syntax:
We enclose all the information in curly braces and specify the key name (like "courses") inside quotation marks. The value associated with this key, in our case, is an array (["Math", "Science"]
), which is enclosed in square brackets to indicate that it is an array.
If we want to transmit simple primitive types (such as numbers, strings or boolean values) in JSON, we can do it like this:
We can transmit objects in JSON format. An object is represented by a set of key-value pairs enclosed in curly braces.
Each key is a string and is followed by a colon, with the corresponding value, which can be another object, an array, a primitive type, or null.
Transmitting JSON over HTTP
When transmitting data in JSON format via HTTP, both requests and responses use the Content-Type
header set to application/json
. In a request, the data is placed in the body of the request, while in a response, the data is placed in the body of the response.
Request Structure:
Response Structure:
JSON Serialization and Deserialization
Serialization converts an object or data structure into a format that is suitable for network transmission or file storage.
Deserialization is the reverse process, which converts data from JSON format back into an object or data structure for use in the code.
Dependency from the video for integrating serialization and deserialization of JSON. This is necessary so that we can use the ObjectMapper
class.
You need to insert this dependency into the pom.xml
file and update it.
How to use the Maven Repository
What is XML?
XML represents data using tags that form a hierarchical structure. Each element has both opening and closing tags and may also include attributes.
XML Syntax
As you can see, everything here is built on tags. Lists and objects are not inherently different, but if we want to represent a simple value, the tag name will serve as the key, and the value inside the tags will be the value.
XML Transmission Over HTTP:
For transmitting XML data, the Content-Type
header can be set to either application/xml
or text/xml
. The data is also placed in the body of the request or response.
Example of a Request:
Example of a Response:
XML Serialization and Deserialization
Dependency from the video for integrating serialization and deserialization of XML. This is necessary so that we can use the XmlMapper
class.
You need to insert this dependency into the pom.xml
file and update it.
Summary
JSON is the preferred choice for modern web applications due to its lightweight and simple nature. While XML may be less convenient for many web scenarios, it can be useful in cases that require a complex data structure and strict schemas.
Дякуємо за ваш відгук!