如何自定义Spring Boot隐式使用的Jackson JSON映射器? [英] How to customise the Jackson JSON mapper implicitly used by Spring Boot?

查看:361
本文介绍了如何自定义Spring Boot隐式使用的Jackson JSON映射器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot(1.2.1),其方式与相似构建RESTful Web服务教程:

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

@RestController
public class EventController {

    @RequestMapping("/events/all")
    EventList events() {
        return proxyService.getAllEvents();
    }

}

因此,Spring MVC暗中使用Jackson将我的 EventList 对象序列化为JSON。

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

但我想对JSON格式进行一些简单的自定义,例如:

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

setSerializationInclusion(JsonInclude.Include.NON_NULL)

问题是,自定义隐式JSON映射器的最简单方法是什么?

我在 此博文中尝试了该方法/ strong>,创建一个CustomObjectMapper等,但步骤3在Spring上下文中注册类失败:

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:

org.springframework.beans.factory.BeanCreationException: 
  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: {}

看起来这些指令适用于旧版本的Spring MVC,而我我正在寻找一种简单的方法来使用最新的Spring Boot。

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.

推荐答案

如果您使用的是Spring Boot 1.3,那么可以通过 application.properties 配置序列化包含:

If you're using Spring Boot 1.3, you can configure serialisation inclusion via application.properties:

spring.jackson.serialization-inclusion=non_null

在Jackson 2.7中进行更改后,Spring Boot 1.4使用名为<$ c的属性$ c> spring.jackson.default-property-inclusion 而不是:

Following changes in Jackson 2.7, Spring Boot 1.4 uses a property named spring.jackson.default-property-inclusion instead:

spring.jackson.default-property-inclusion=non_null

参见自定义Jackson ObjectMapper 部分在Spring启动文档。

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

如果你使用早期版本的Spring Boot,在Spring Boot中配置序列化包含的最简单方法是声明自己的,适当配置的 Jackson2ObjectMapperBuilder bean。例如:

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:

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.serializationInclusion(JsonInclude.Include.NON_NULL);
    return builder;
}

这篇关于如何自定义Spring Boot隐式使用的Jackson JSON映射器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆