Spring Boot — Configuring Spring Boot to use Gson instead of Jackson | Code Factory
3 min readNov 4, 2020
Donate : Link
WordPress Blog : Link
Applications… : Link
- Spring Boot uses Jackson by default for serializing and deserializing request and response objects in REST APIs.
- If you want to use GSON instead of Jackson then it’s just a matter of adding
Gsondependency in yourpom.xmlfile and specifying a property in theapplication.propertiesfile to tell Spring Boot to useGsonas preferred json mapper.
Force Spring Boot to use GSON instead of Jackson
1. Add Gson dependency
- Add the GSON dependency in
pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>- After that, Spring Boot will detect
Gsondependency on the classpath and automatically create aGsonbean with sensible default configurations. You can also autowiregsonin 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
GsonAutoConfigurationclass. Notice how it uses@ConditionalOnClass(Gson.class)annotation to trigger the auto-configuration whenGsonis available on the classpath. - Jackson is also configured in a similar fashion with
JacksonAutoConfigurationclass.
2. Set the preferred json mapper to gson
- You can now ask Spring Boot to use
Gsonas your preferred json mapper by specifying the following property in theapplication.propertiesfile
# Preferred JSON mapper to use for HTTP message conversion.
spring.http.converters.preferred-json-mapper=gsonThat’s all you need to do to force Spring Boot to use Gson instead of Jackson.
Configure GSON in Spring Boot
- Now that your Spring Boot application is using
Gson, you can configureGsonby specifying various properties in theapplication.propertiesfile. For more common application properties please look Common application properties in Spring Boot
# 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.
- All the above properties are bound to a class called
GsonPropertiesdefined in Spring Boot. TheGsonAutoConfigurationclass uses these properties to configure Gson.
Excluding Jackson completely
- If you want to get rid of Jackson completely then you can exclude it from
spring-boot-starter-webdependency in thepom.xmlfile
<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>