Configure Spring Boot with Gson or Jackson or JSON-B

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.

Let's discuss each of the above JSON library integration with Spring boot.

1. Jackson

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.

2. Gson

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 and discuss 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.

Add Gson dependency

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;

Set the preferred JSON mapper to 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.

3 JSON-B

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

Add JSON-B dependency

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>

Set the preferred JSON mapper to JSON-B

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.

Related Spring Boot Articles


Check out all spring boot articles, guides, and tutorials at Top Spring Boot Tutorials

Comments