Configure Spring Boot with Gson or Jackson or JSON B - RameshMF/spring-boot-developers-guide GitHub Wiki
In this short article, we will discuss how to configure Spring Boot to work with different JSON mapping libraries.
Spring Boot provides integration with three JSON mapping libraries:
- Gson
- Jackson
- JSON-B Jackson is the preferred and default library.
Spring Boot uses Jackson by default for serializing and deserializing request and response objects in your REST APIs.
Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json. When Jackson is on the classpath an ObjectMapper bean is automatically configured. Several configuration properties are provided for customizing the configuration of the ObjectMapper.
Auto-configuration for Gson is provided. When Gson is on the classpath a Gson bean is automatically configured. Several spring.gson.* configuration properties are provided for customizing the configuration.
Let's deep dive how to use GSON in your spring boot projects.
If you want to use GSON instead of Jackson then it’s just a matter of adding Gson dependency in your pom.xml file and specifying a property in the application.properties file to tell Spring Boot to use Gson as your preferred JSON mapper.
Open your pom.xml file and add the GSON dependency like so -
<!-- Include GSON dependency -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
Once you do that, Spring Boot will detect Gson dependency on the classpath and automatically create a Gson bean with sensible default configurations. You can also autowire GSON in your spring components directly like so -
@Autowire
private Gson gson;
You can now ask Spring Boot to use Gson as your preferred JSON mapper by specifying the following property in the application.properties file -
# Preferred JSON mapper to use for HTTP message conversion.
spring.http.converters.preferred-json-mapper=gson
That’s all you need to do to force Spring Boot to use Gson instead of Jackson.
JSON Binding (JSON-B) is the new Java EE specification for converting JSON messages to Java Objects and back.
Auto-configuration for JSON-B is provided. When the JSON-B API and implementation are on the classpath a Jsonb bean will be automatically configured. The preferred JSON-B implementation is Apache Johnzon for which dependency management is provided.
If you want to know more about JSON-B itself, you can visit the official website - http://json-b.net/index.html
Let's see how to use JSON-B in your spring boot projects. It is very simple. You need to add the required Maven dependencies:
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
And you need to choose the preffered-json-mapper setting to make sure that JSON-B is chosen. You may get GSON or Jackson on the classpath and then you can’t be sure how the autoconfiguration will work without this setting:
spring.http.converters.preferred-json-mapper=jsonb
That’s all you need to do to force Spring Boot to use JSON-B instead of Jackson.