Learn to create Spring Boot REST services that accept the requests and produce the responses in JSON format. We will delve into various aspects, including serialization, deserialization, customization, and the integration of third-party libraries for handling JSON.
1. JSON Support in Spring Boot
Spring Boot provides integration for the following JSON mapping libraries:
- Jackson (Default)
- Gson
In Spring Boot, Jackson is the preferred and default library for JSON serialization and deserialization.
When a Java object needs to be sent as a JSON response, Jackson automatically converts it into a JSON representation. Similarly, when JSON data is received in a request, Spring Boot automatically converts it into the corresponding Java object.
Note that we can still use other lightweight JSON libraries (e.g. Fastjson, JSON-B, Boon, Flexjson, LoganSquare or Moshi) when the requirements arise.
2. Using Jackson for JSON Requests and Responses
Spring Boot provides various ways to customize the default serialization and deserialization process. Let’s explore in detail.
2.1. Default Autoconfiguration
Spring boot, by default, includes Jackson dependency and is part of spring-boot-starter-json. Using JacksonAutoConfiguration class, spring boot automatically configures Jackson with the following behavior:
- The ObjectMapper bean in case none is already configured.
- The Jackson2ObjectMapperBuilder bean in case none is already configured.
- Auto-registration for all
Module
beans with all ObjectMapper beans (including the defaulted ones).
2.2. Producing the JSON Response
Any Spring @RestController in a Spring Boot application should render JSON response by default as long as Jackson is on the classpath.
In the given example, EmployeeList will be serialized by Jackson and serve as a JSON representation to the client.
Similarly, for creating or updating, the client can send the JSON payload in the request body. In the given example, Employee the object will be populated with JSON request.
2.3. Customizing Jackson ObjectMapper
Spring boot auto-configures MappingJackson2HttpMessageConverter as one of the default converters to handle request and response body conversion.
We can customize the default conversion behavior using either property files or custom bean definitions.
2.3.1. Property Configuration
Use the relevant properties to configure the desired behavior of ObjectMapper bean.
2.3.2. Java Configuration
Use anyone of the below bean configurations to override JSON behavior in spring boot.
3. Using Gson with JSON Serialization/Deserialization
Spring Boot allows you to integrate with third-party JSON libraries if needed. For example, if you prefer Gson over Jackson, you can exclude Jackson and include Gson in your project:
Spring boot detects the presence of Gson.class and uses GsonAutoConfiguration for configuring the Gson instance.
By default, Spring Boot will try to auto-configure a JSON mapper based on the libraries present in the classpath. If only one supported mapper is available, Spring Boot will use it as the default. If multiple mappers are present,we need to specify the default mapper explicitly.
For example, to make Gson the preferred JSON mapper, use the ‘spring.mvc.converters.preferred-json-mapper‘ property in application.properties file.
See Also: Gson with Spring Boot
We can customize the various aspects of serialization and deserialization using Gson using the following properties:
4. Conclusion
In Spring Boot, Jackson is the default JSON mapper library used. Also, note that Spring Boot provides the flexibility in choosing a JSON library,
In most cases, it might be more straightforward to rely on the default configuration. If you have specific requirements or preferences, you can explicitly configure the preferred JSON mapper using the property mentioned above.
Drop me your questions related to producing and consuming JSON in Spring Boot REST APIs.
Happy Learning !!
Comments