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.
Introduction
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 application.properties
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:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version> <!-- check latest version for GSON -->
</dependency>
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 application.properties
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.
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 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.
spring.gson.exclude-fields-without-expose-annotation=
# Naming policy that should apply 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=
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.
<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>
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) {
SpringApplication.run(GsonSpringBootApplication.class, 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.
@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customGsonHttpMessageConverter());
super.configureMessageConverters(converters);
}
private GsonHttpMessageConverter customGsonHttpMessageConverter() {
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'")
.create();
GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
gsonMessageConverter.setGson(gson);
return gsonMessageConverter;
}
}
4. Unit Test
Let’s set up a small unit test case to test out Gson convertor.
@RunWith(SpringRunner.class)
@SpringBootTest
public class GsonSpringBootApplicationTests {
private Product product;
@Before
public void setup(){
product =new Product("123","Demo Product",123);
}
@Test
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);
JSONAssert.assertEquals(expected,data,false);
}
@Test
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);
JSONAssert.assertEquals(expected,data,false);
}
}
On running this unit test, we may see something like:
Summary
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
not working
Can you provide more information as what is not working?