如何在Spring Boot中注册和使用jackson AfterburnerModule? [英] How can I register and use the jackson AfterburnerModule in Spring Boot?

查看:223
本文介绍了如何在Spring Boot中注册和使用jackson AfterburnerModule?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpringBoot 1.5.9.,Jackson 2.8和Spring Framework 4.3.13.

I am using SpringBoot 1.5.9., Jackson 2.8 and Spring Framework 4.3.13.

我正在尝试注册和使用AfterburnerModel.

I am trying to register and use the AfterburnerModel.

根据Spring Boot文档,要配置ObjectMapper,您可以自己定义bean并用@Bean和@Primary对其进行注释.在bean中,您可以注册一个模块.或者,您可以添加类型为Jackson2ObjectMapperBuilder的bean,在其中可以通过添加模块来自定义ObjectMapper.

According to the Spring Boot documentation, to configure the ObjectMapper you can either define the bean yourself and annotate it with @Bean and @Primary. In the bean you can register a module. Or you can add a bean of type Jackson2ObjectMapperBuilder where you can customize the ObjectMapper, by adding a module.

我尝试了两种方式,并且在序列化过程中,杰克逊模块加力燃烧器中没有断点.我的自定义设置已被读取,但似乎被忽略了.

I have tried both ways, and during serialization none of my breakpoints in jackson-module-afterburner fire. My customizations are being read, but seem to be being ignored.

推荐答案

默认情况下,Spring MVC MappingJackson2HttpMessageConverter将使用Jackson2ObjectMapperBuilder创建带有默认选项的自己的ObjectMapper.根据Spring Boot docs 76.3自定义Jackson ObjectMapper 章节:

By default Spring MVC MappingJackson2HttpMessageConverter will create it's own ObjectMapper with default options using Jackson2ObjectMapperBuilder. As per Spring Boot docs 76.3 Customize the Jackson ObjectMapper chapter:

任何类型为com.fasterxml.jackson.databind.Module的bean都会自动注册到自动配置的Jackson2ObjectMapperBuilder中,并应用于它创建的任何ObjectMapper实例.当您向应用程序中添加新功能时,这提供了一种用于贡献自定义模块的全局机制.

Any beans of type com.fasterxml.jackson.databind.Module are automatically registered with the auto-configured Jackson2ObjectMapperBuilder and are applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.

因此足以将您的模块注册为Bean:

so it should be enough to register your module as a bean:

@Bean
public AfterburnerModule afterburnerModule() {
  return new AfterburnerModule();
}

可以使用@Configuration类自定义MappingJackson2HttpMessageConverter来实现更详细的设置:

A more detailed setup can be achieved with @Configuration class to customize the MappingJackson2HttpMessageConverter:

@Configuration
public class MyMvcConf extends WebMvcConfigurationSupport {

  protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(myConverter());
    addDefaultHttpMessageConverters(converters);
  }

  @Bean
  public MappingJackson2HttpMessageConverter myConverter() {
    return new MappingJackson2HttpMessageConverter(myObjectMapper())
  }

  @Bean
  public ObjectMapper myObjectMapper() {
    return new ObjectMapper().registerModule(new AfterburnerModule());
  }

}

这篇关于如何在Spring Boot中注册和使用jackson AfterburnerModule?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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