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

查看:37
本文介绍了如何自定义 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 映射器的最简单方法是什么?

我在这篇博文,创建一个 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.

推荐答案

您可以通过 application.properties 配置属性包含和许多其他设置:

You can configure property inclusion, and numerous other settings, via application.properties:

spring.jackson.default-property-inclusion=non_null

文档 列出了所有可以使用的属性.

There's a table in the documentation that lists all of the properties that can be used.

如果您想要更多控制,您还可以使用 Jackson2ObjectMapperBuilderCustomizer bean 以编程方式自定义 Spring Boot 的配置,如 文档:

If you want more control, you can also customize Spring Boot's configuration programatically using a Jackson2ObjectMapperBuilderCustomizer bean, as described in the documentation:

上下文的 Jackson2ObjectMapperBuilder 可以由一个或多个 Jackson2ObjectMapperBuilderCustomizer bean 自定义.可以订购此类定制器 bean(Boot 自己的定制器的 order 为 0),从而在 Boot 定制之前和之后应用额外的定制.

The context’s Jackson2ObjectMapperBuilder can be customized by one or more Jackson2ObjectMapperBuilderCustomizer beans. Such customizer beans can be ordered (Boot’s own customizer has an order of 0), letting additional customization be applied both before and after Boot’s customization.

最后,如果您不想要任何 Boot 的配置并希望完全控制 ObjectMapper 的配置方式,请声明您自己的 Jackson2ObjectMapperBuilder bean:

Lastly, if you don't want any of Boot's configuration and want to take complete control over how the ObjectMapper is configured, declare your own Jackson2ObjectMapperBuilder bean:

@Bean
Jackson2ObjectMapperBuilder objectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    // Configure the builder to suit your needs
    return builder;
}

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

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