Jonik Jonik - 1 year ago 387
Java Question

How to customise the Jackson JSON mapper implicitly used by Spring Boot?

I'm using Spring Boot (1.2.1), in a similar fashion as in their Building a RESTful Web Service tutorial:

public class EventController {

EventList events() {
return proxyService.getAllEvents();


So above, Spring MVC implicitly uses Jackson for serialising my
object into JSON.

But I want to do some simple customisations to the JSON format, such as:


Question is, what is the simplest way to customise the implicit JSON mapper?

I tried the approach in this blog post, creating a CustomObjectMapper and so on, but the step 3, "Register classes in the Spring context", fails:

Error creating bean with name 'jacksonFix': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire method: public void com.acme.project.JacksonFix.setAnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter);
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

It looks like those instructions are for older versions of Spring MVC, while I'm looking for a simple way to get this working with latest Spring Boot.

Answer Source

If you're using Spring Boot 1.3 or newer, you can configure serialisation inclusion via


See "Customize the Jackson ObjectMapper" section in Spring Boot documentation.

If you're using an earlier version of Spring Boot, the easiest way to configure serialisation inclusion in Spring Boot is to declare your own, appropriately configured Jackson2ObjectMapperBuilder bean. For example:

public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    return builder;

You can also use the normal spring-boot-configuration and put into the config:

@Autowired(required = true)
public void configeJackson(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download