1. Overview

In this article, We will learn how to configure Gson in spring boot application or we can say it is a way to configure Gson instead of Jackson.

Spring boot by default uses Jackson to convert Java objects to JSON or XML, but it also provides easy ways to switch Jackson to Gson library. Gson is widely used library for serialize and de-serialize Java Object to JSON.

It’s always difficult to say which one is better, generally based on project requirement and individual preferences, we can us Jackson or Gson library.

Now let’s learn how to configure Gson in spring boot application:

2. Steps to Configure Gson in spring boot

1.1 add Gson dependency in pom.xml

First of all add dependency library in pom.xml or gradle file and make sure that library version is supported by spring boot version.

<dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
</dependency>

1.2 Set JSON preference in application.properties or application.yml

After adding Gson dependency in our project, we have two libraries in our project for conversion, so we need to specify which library we want to use.

We can use spring.http.converters.preferred-json-mapper property in application.propertiesfile for specifying the exact library that we want to use.

Here possible configuration values are: gson, jackson, jsonb

application.properties

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

or

application.yml

spring:
  http:
    converters:
      preferred-json-mapper: gson

3. Step to Configure Gson and exclude Jackson from spring boot

If we do not require Jackson Library then we should exclude it so we can prevent conflicts between Gson and Jackson.

Even I like to recommend that we have to remove Jackson if we are planning to use only Gson in project because using two different library for JSON conversion is not preferable way in same project.

Sometimes both libraries have different ways to represent same object in different way so it might create confusion for end user who uses API or JSON.

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <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>

After configuring Gson library, we can change configuration related to serialize and de-serialize using spring.gson.* in application.propertiesor application.yml:

Here are some important Gson properties which may help to customize Gson configurations:

# GSON (GsonProperties)
spring.gson.date-format= # Format to use when serializing Date objects.
spring.gson.disable-html-escaping= # Whether to disable the escaping of HTML characters such as '<', '>', etc.
spring.gson.disable-inner-class-serialization= # Whether to exclude inner classes during serialization.
spring.gson.enable-complex-map-key-serialization= # Whether to enable serialization of complex map keys (i.e. non-primitives).
spring.gson.exclude-fields-without-expose-annotation= # Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation.
spring.gson.field-naming-policy= # Naming policy that should be applied to an object's field during serialization and deserialization.
spring.gson.generate-non-executable-json= # Whether to generate non executable JSON by prefixing the output with some special text.
spring.gson.lenient= # Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.
spring.gson.long-serialization-policy= # Serialization policy for Long and long types.
spring.gson.pretty-printing= # Whether to output serialized JSON that fits in a page for pretty printing.
spring.gson.serialize-nulls= # Whether to serialize null fields.

4. Conclusion

In this article, We learnt that how we can forcefully configure Gson in spring boot application and exclude Jackson configuration for JSON conversion, and also learnt useful properties related to Gson which help us to customize JSON according to requirements.Here is complete example of create Json rest service in spring boot.

5. Refrences

6. Source Code

Git : spring-boot-gson-example

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No