Jackson has been the default json library in springframework until version 4.1 where it added support to use Gson by configuring GsonHttpMessageConverter. Let's take a look at how to configure your spring application to use Google Gson library's Gson class.
Detailed Video Notes
Gson is a java based library that converts java objects into their JSON representation and vice versa. A common example is when you are create a REST end point via a spring @Controller
where you fetch a list of objects that you want to convert into an jsonarray. Let's examine a few different configuration options within your spring boot, java config and xml based applications.
Getting started
[0:21]
Before we get started it is important to understand how spring converts objects to json. In typical spring mvc once request exits the @Controller
it looks for a view to render. When specifying a @RequestBody
or a @RestController
we ask spring to by pass this step and write the java objects in the model directly to the response. When doing so spring will look specifically for a HttpMessageConverter
associated to the mime type to perform the conversion and in our case Json. Spring configures MappingJackson2HttpMessageConverter
by default so we want to swap it with GsonHttpMessageConverter
so it uses Gson to convert the java objects.
For our first snippet, lets generate a spring boot application from spring initializr web page and import it into eclipse.
Configuring gson in spring boot
[1:1]
Adding gson to classpath
If you are setting up Gson to work with spring boot you will first want to look at HttpMessageConvertersAutoConfiguration
as there may be configuration changes. As it exists during the write up of this tutorial the GsonHttpMessageConverter
will be created if Gson is on your classpath, the application doesn't contain jackson's JsonGenerator class and if the Gson bean doesn't exist already.
Excluding jackson from classpath
[1:29]
If you want to be sure that jackson isn't used or if you are experiencing conflicts you can add @EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class })
to your application class and exclude it from the spring-boot-starter-web
dependency.
If you are trying to eliminate the dependency on jackson we did notice as we worked through this tutorial that it was needed if you are using spring boot actuator specifically in EndpointAutoConfiguration
class and it is a reported github issue.
Validate request
Let's create a controller that returns a hashmap to validate that GsonHttpMessageConverter
is being used. We will modify our application.properties to include logging.level.org.springframework=DEBUG
and make a request to /
default request mapping. Inspecting the log we can see that the GsonHttpMessageConverter
is being used.
Customize converters using HttpMessageConverters
[2:14]
If you are having troubles with configuration just discussed or interested in customizing an existing converter by overriding a bean, spring-boot provides an alternative configuration option. This method will also allow you to have both gson and jackson on your class path. We will create a CustomConfiguration
class but this could be performed in any class that contains the @Configuration
annotation and create a new bean of type HttpMessageConverters
. In our configuration we will instruct springframework to use the default HttpMessageConverter
and then append GsonHttpMessageConverter
.
If we include the gson pom dependency and specify the customConverters it will configure GsonHttpMessageConverter
to be used. Now you might be asking, if the jackson is also included on the classpath, how does this work? The default behavior of HttpMessageConverters
when adding a new converter is to add it to the front of the list so since jackson is configured before it works.
Using java config
[3:7]
If you haven't moved to spring boot yet you still configure gson within your application using javaconfig by extending WebMvcConfigurerAdapter
or if you need more control use WebMvcConfigurationSupport
. You can read more on on how to customize the provided spring mvc configuration.
Configure Gson using XML configuration
[3:26]
I didn't add a specific test scenario for configuring gson in a xml based application but it would look something similar to the following:
There has been a lot of debates on which java library is the fastest JSON processor and many of them might loose out to java 8 json api once support broadens. So until then pick a method and library above.
Thanks for joining in today's level up, have a great day!