Gson with Spring Boot

In this article, we will learn how to use Google Gson with Spring Boot. Gson is an open-source Java library to serialize and deserialize Java objects to JSON.



Spring Boot uses Jackson as the default library to serialize and deserialize Java objects to JSON. If you add “spring-boot-starter Spring Boot Starters in your application, it will be included to your classpath. This is great but sometime you may like to use other API than what is available as part of the Spring Boot auto-configuration. In this article we will walk through the steps for using Gson with Spring Boot.

Spring Boot is an intelligent system with certain defaults, it has an auto-configuration for Gson. Spring Boot will auto-configure a Gson bean once it finds Gson is on the classpath. It also provides several Gson specific properties in the file.


1. Maven Dependency

The first step for our configuration is to add Gson dependency in our pom.xml file. This is how our pom.xml file looks like:

  <version>2.8.5</version> <!-- check latest version for GSON -->

With above configuration, Spring Boot creates a Gson bean with sensible defaults. Spring provides a GsonHttpMessageConverter that can read and write JSON using the Google Gson library.


1.1 Using Gson as default Mapper

We included the Gson to the class path but we require setting Gson as the preferred mapper using the file.

spring.http.converters.preferred-json-mapper=gson #Preferred JSON mapper to use for HTTP message conversion.

[pullquote align=”normal”]If you don’t set the preferred json mapper, you may face org.springframework.http.converter.HttpMessageNotWritableException. [/pullquote]


1.2 Gson Configurations

Spring Boot provides several properties for Gson configurations. Here is the list reference:

# Format to use when serializing Date objects. 

# Whether to disable the escaping of HTML characters such as '<', '>', etc.

# Whether to exclude inner classes during serialization.

# Whether to enable serialization of complex map keys (i.e. non-primitives).
spring.gson.enable-complex-map-key-serialization= # Whether to enable serialization of complex map keys (i.e. non-primitives).

# Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation.

# Naming policy that should apply to an object's field during serialization and deserialization.

# Whether to generate non executable JSON by prefixing the output with some special text.

# Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.

# Serialization policy for Long and long types.

# Whether to output serialized JSON that fits in a page for pretty printing.

# Whether to serialize null fields.

Please refer to the official documents for update properties.


2. Exclude Jackson Dependency

If you will use Gson as your default library, remove the Jackson from the classpath. There are two ways to exclude it from the classpath:


2.1 Using Maven

The easiest way is to use the exclude tag in your pom.xml. Spring Boot add Jackson as part of the web starter, all we need is to exclude it in the web starter.

        <!-- Exclude the default Jackson dependency -->


2.1 Using Exclude property

Second alternate is to use the exclude property with @EnableAutoConfiguration or  @SpringBootApplication

@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
public class GsonSpringBootApplication {

    public static void main(String[] args) {, args);

With this option, you can skip setting spring.http.converters.preferred-json-mapper as there will be only one mapper configured by Spring Boot


3. Customize Using HttpMessageConverters

To customize behaviour of the Gson mapper in your Spring Boot application, you can extend WebMvcConfigurerAdapter to get hold of the Http Message Converters with the Spring.Let’s take an example where we want to customize the date format for our JSON convertor.

public class ApplicationConfig extends WebMvcConfigurerAdapter {

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    private GsonHttpMessageConverter customGsonHttpMessageConverter() {
        Gson gson = new GsonBuilder()

        GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();

        return gsonMessageConverter;


4. Unit Test

Let’s set up a small unit test case to test out Gson convertor.

public class GsonSpringBootApplicationTests {

    private Product product;

    public void setup(){
        product =new  Product("123","Demo Product",123);

    public void simpleGsonTest() throws JSONException {
        String expected = "{\n" +
                "\"code\": \"123\",\n" +
                "\"name\": \"Demo Product\",\n" +
                "\"price\": 123\n" +

        Gson gson = new GsonBuilder().create();
        String data= gson.toJson(product);


    public void errorGsonTest() throws JSONException {
        String expected = "{\n" +
                "\"code\": \"1233\",\n" +
                "\"name\": \"Demo Product\",\n" +
                "\"price\": 123\n" +

        Gson gson = new GsonBuilder().create();
        String data= gson.toJson(product);


On running this unit test, we may see something like:

Spring Boot to use Gson



In this post we talk about how to use Gson with Spring Boot. We leaned how to configure Spring Boot to use Gson as the Json processor along with steps to customize the behaviour of the processor using HttpMessageConverters. You can download the source code of this post from GitHub

2 thoughts on “Gson with Spring Boot”

Comments are closed.