pring Boot uses Jackson by default for serializing and deserializing request and response objects in your REST APIs.
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.
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;
If you’re curious how Spring Boot does that, then take a look at this
GsonAutoConfiguration class. Notice how it uses
@ConditionalOnClass(Gson.class) annotation to trigger the auto-configuration when
Gson is available on the classpath.
Jackson is also configured in a similar fashion with
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.
Now that your Spring Boot application is using
Gson, you can configure
Gson by specifying various properties in the
application.properties file. The following properties are taken from Spring Boot Common Application Properties index page –
# GSON (GsonProperties) # 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=
All the above properties are bound to a class called
GsonProperties defined in Spring Boot. The
GsonAutoConfiguration class uses these properties to configure Gson.
If you want to get rid of Jackson completely then you can exclude it from
spring-boot-starter-web dependency in the
pom.xml file like so –
<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>
I hope you enjoyed this article. Thanks for reading. See you next time!