如何在 Spring Boot 1.4 中自定义 Jackson [英] How to customise Jackson in Spring Boot 1.4

查看:39
本文介绍了如何在 Spring Boot 1.4 中自定义 Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直找不到有关如何在 spring boot 1.4 中使用 Jackson2ObjectMapperBuilderCustomizer.java 来自定义 Jackson 功能的示例.

I've been unable to find examples of how to use Jackson2ObjectMapperBuilderCustomizer.java in spring boot 1.4 to customise the features of Jackson.

用于在 boot 1.4 中自定义 Jackson 的 doco - https://docs.spring.io/spring-boot/docs/1.4.x/reference/htmlsingle/#howto-customize-the-jackson-objectmapper

The doco for customising Jackson in boot 1.4 - https://docs.spring.io/spring-boot/docs/1.4.x/reference/htmlsingle/#howto-customize-the-jackson-objectmapper

我的配置有效,但我不确定这是否是使用 Jackson2ObjectMapperBuilderCustomizer.java

My configuration works, although I am unsure if this is the correct way to customise the object mapper using Jackson2ObjectMapperBuilderCustomizer.java

@Configuration
public class JacksonAutoConfiguration {

    @Autowired
    private Environment env;

    @Bean
    public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(
        List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
        Jackson2ObjectMapperBuilder builder = configureObjectMapper();
        customize(builder, customizers);
        return builder;
    }

    private void customize(Jackson2ObjectMapperBuilder builder,
                           List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
        for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
            customizer.customize(builder);
        }
    }

    private Jackson2ObjectMapperBuilder configureObjectMapper() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        List<String> activeProfiles = asList(env.getActiveProfiles());
        if (activeProfiles.contains(SPRING_PROFILE_DEVELOPMENT)) {
            builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
        }
        return builder;
    }
}

为了提供一些上下文,这个类位于我自己的 REST 服务的 spring 启动项目中,它只是自动配置一些东西,比如 ControllerAdvice 和一些像上面这样的琐碎功能.

To provide some context, this class sits in my own spring starter project for REST services that just auto configures a number of things, like ControllerAdvice and some trivial features like the above.

所以我的目标是扩展 Jackson 配置,而不是覆盖 boot 或其他包提供的任何配置.

So my goal is to extend the Jackson configuration rather than to override any configuration provided by boot or other packages.

推荐答案

为了自定义已经由 Spring Boot 预先配置的 Jackson ObjectMapper,我能够做到这一点(这里的例子是添加自定义解串器).

To customize the Jackson ObjectMapper that's already pre-configured by Spring Boot, I was able to do this (the example here is to add a custom deserializer).

配置类:

@SpringBootConfiguration
public class Application {

    @Autowired
    private BigDecimalDeserializer bigDecimalDeserializer;

    ...

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
        return new Jackson2ObjectMapperBuilderCustomizer() {

            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.deserializerByType(BigDecimal.class, bigDecimalDeserializer);
            }

        };
    }

    ...

}

还有我的自定义解串器,以展示它是如何被 Spring 接收的:

And my custom deserializer, to show how it's picked up by Spring:

@Component
public class BigDecimalDeserializer extends StdDeserializer<BigDecimal> {

    public BigDecimalDeserializer() {
        super(BigDecimal.class);
    }

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        ...
    }

    ...

}

这篇关于如何在 Spring Boot 1.4 中自定义 Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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