Gson with Spring boot 2

Learn to configure Gson with spring boot applications as preferred json mapper. By default, Spring boot uses Jackson for this purpose.

1. Include Gson dependency

Include gson in the spring boot application by adding the appropriate Gson dependency.

dependencies {
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}
<dependencies>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>
</dependencies>

2. Gson auto-configuration

With dependency inclusion, Spring Boot detects Gson dependency on the classpath and creates a Gson bean with sensible defaults.

2.1. Default Gson instance

Spring boot detects presence of Gson.class and uses GsonAutoConfiguration for configuring the Gson instance.

If we need direct access to Gson instance, we can directly autowire in spring beans:

@Autowire
private Gson gson;

2.2. Custom Gson instance

To customize the default behavior of Gson instance, configure the respective property from below list. GsonAutoConfiguration uses these properties while initializing the Gson instance.

# Format to use when serializing Date objects.
spring.gson.date-format= 

# Whether to disable the escaping of HTML characters such as '<', '>', etc.
spring.gson.disable-html-escaping= 

# Whether to exclude inner classes during serialization.
spring.gson.disable-inner-class-serialization= 

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

# Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation.
spring.gson.exclude-fields-without-expose-annotation= 

# Naming policy that should be applied to an object's field during serialization and deserialization.
spring.gson.field-naming-policy= 

# Whether to generate non executable JSON by prefixing the output with some special text.
spring.gson.generate-non-executable-json= 

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

# Serialization policy for Long and long types.
spring.gson.long-serialization-policy= 

# Whether to output serialized JSON that fits in a page for pretty printing.
spring.gson.pretty-printing= 

# Whether to serialize null fields.
spring.gson.serialize-nulls= 

3. Make Gson preferred json mapper

To configure spring boot to use Gson as preferred mapper over Jackson, use this property in application.properties file.

spring.http.converters.preferred-json-mapper=gson

Now even if Jackson is available in classpath, Spring boot will use Gson to serialize and deserialize JSON payloads.

4. Exclude Jackson completely

If we want to exclude Jackson completely from application runtime, we can do it by exluding spring-boot-starter-json.

4.1. Exclude Jackson from project dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- Exclude the default Jackson dependency -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>

4.2. Disable auto-configuration

If we want to exclude Jackson only inside spring boot configuration, we can exclude it by disabling it’s auto configuration class JacksonAutoConfiguration.

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

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class, args);
    }
}

Drop me your questions related to configure Gson with Spring boot 2.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.