Spring MVC & JSON Serialization

Spring uses Jackson internally for serializing and deserializing data in json format. It just works in most cases. Still, some corner cases can turn up where Spring cannot apply Jackson’s default operations. That is when you must write your own functions.

In this posting I will show you how to customize json serialization in the context of the Spring MVC. The code is available on Github.

The Use Case

I will demonstrate a typical shop example where a customer adds a product to the basket. Below you find the BasketItem class and the Controller. BasketItem is used in request and response. I want to point out the importance of the annotation @RequestBody. If you do not set that, then Spring does not invoke Jackson at all.

The client sends and expects json that is a little bit different from the BasketItem so we need both custom serialization and deserialization:

Jackson relies on the so-called ObjectMapper. If you want customized behavior for certain classes you have to create a Jackson module that contains the relevant logic and register it to the ObjectMapper.

In Spring things are a little bit different. You do not have direct access to the ObjectMapper. You cannot inject it as a bean either. The only solution is to create a Jackson module and mark it as a bean. Spring automatically adds that to the internal ObjectMapper.

Jackson modules are quite powerful in their possibilities. However, if it comes down to just registering a serializer, then the easiest way is to extend from SimpleModule. It includes all the code required to be an actual Jackson module.

Implementing the Serialization Logic

Next we tackle the implementation. As we follow TDD practices, we begin with the unit tests. In the tests we specify that there exists a Jackson module with the name BasketJsonModule which is registered to the ObjectMapper. Afterwards serialization should work as expected:

So far so good. Now we implement the classes according to the unit tests and make sure the tests turn green:

Running the Application

Since unit tests are running fine, our last step will be to test the application as a whole. Please note that the BasketItemJsonModule is already annotated with @Service. Spring automatically registers it to its internal ObjectMapper instance.

From the terminal we run:

After Spring is running we open a second terminal and execute the following curl command:

If the json structure with the right values is returned, you have succeeded.

Alternative Approach: Using @JsonSerialize and @JsonDeserialize

A short cut sets the serializer and deserializer by adding the following annotations to the BasketItem class:

This approach is shorter and very straightforward since you do not require a Jackson module. In my opinion, however, it should only be used for simple applications. In systems composed of multiple modules, you do not want to spread dependencies to Jackson. It is also quite possible that you will not have access to these classes in order to set the annotations.

Why ObjectMapper cannot be accessed directly

When first tackling this problem my strategy was to find ways to access the ObjectMapper directly. After all, Spring lets you inject virtually everything. That seemed the natural way to me.

The problem is, Spring itself creates a new instance of the ObjectMapper in each request. Therefore you can only provide a Factory class as a bean for the ObjectMapper. I do not recommend doing that. The Spring team knows the best configuration that integrates and works well with the rest of the Spring ecosystem.

If you are still curious you can check out the AbstractJackson2HttpConverter and classes it uses. The documentation is also worth a check.

I will repeat this as my closing advice: Don’t miss the @RequestBody in the Controller.

Leave a Reply