如何在 Spring Boot 中为 Camel 配置 Jackson ObjectMapper [英] How to configure Jackson ObjectMapper for Camel in Spring Boot

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

问题描述

我正在尝试使用 Jackson 在 Camel 路由上将 POJO 序列化和反序列化到 JSON.其中一些具有 Java 8 LocalDate 字段,我希望它们被序列化为 YYYY-MM-DD 字符串,而不是整数数组.

I am trying to serialize and deserialize POJOs to and from JSON on Camel routes using Jackson. Some of these have Java 8 LocalDate fields, and I want them to be serialised as YYYY-MM-DD string, not as an array of integers.

我们只对 Spring Boot 应用程序使用 Java 配置,所以没有 XML Camel 配置.

We only use Java configuration for our Spring Boot application, so no XML Camel configuration.

我已经成功地创建了一个 ObjectMapper,它可以做我想做的事,我们系统的其他部分正在通过将它添加到我们的依赖项中来使用它:

I have successfully created an ObjectMapper that does what I want, which is being used by other parts of our system by adding this to our dependencies:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

这是我们的应用程序配置:

and this to our application configuration:

@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    return builder
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .build();
}

示例传出 REST 路由:

Example outgoing REST route:

@Component
public class MyRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        restConfiguration().component("servlet").contextPath("/mycontext")
                .port(8080).bindingMode(RestBindingMode.json);

        rest("/myendpoint)
                .get()
                .route()
                .to("bean:myService?method=myMethod()");
    }
}

示例传入消息路由:

@Component
public class MyRouteBuilder extends RouteBuilder {

    @Autowired
    private MyBean myBean;

    @Override
    public void configure() {
        from(uri)
                .unmarshal().json(JsonLibrary.Jackson)
                .bean(myBean);
    }
}

但是,默认情况下,Camel 创建自己的 ObjectMapper 实例,因此不会使用 Jackson2ObjectMapperBuilder 自动添加的 JSR310 序列化器/解串器,或禁用的 WRITE_DATES_AS_TIMESTAMPS 功能.我已经阅读了 Camel JSON 文档,但它没有显示如何使用 Spring 添加自定义 DataFormat配置,或如何对所有类型应用全局自定义.

However, by default Camel creates its own ObjectMapper instances so does not pick up on either the JSR310 serializers/deserializers that Jackson2ObjectMapperBuilder adds automatically, or the disabled WRITE_DATES_AS_TIMESTAMPS feature. I have read the Camel JSON documentation, but it does not show how to add a custom DataFormat using Spring configuration, or how to apply a global customisation for all types.

那么如何告诉 Camel 使用我的 ObjectMapper,仅使用 Spring Boot Java 配置?

So how can I tell Camel to use my ObjectMapper, using only Spring Boot Java configuration?

推荐答案

我通过单步执行 Camel 代码找到了解决方案.因此,虽然它可以满足我的要求,但它可能不适用于 Camel 的未来版本,因为它似乎没有记录并且可能不受支持.

I have found a solution by stepping through the Camel code. So while it does what I want, it might not work with future versions of Camel since it appears to be undocumented and potentially unsupported.

除了问题中的 ObjectMapper bean,我所做的就是将以下 bean 添加到我的 Spring 配置中:

All I do is add the following bean to my Spring config, in additional to my ObjectMapper bean in the question:

@Bean(name = "json-jackson")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public JacksonDataFormat jacksonDataFormat(ObjectMapper objectMapper) {
    return new JacksonDataFormat(objectMapper, HashMap.class);
}

需要注意的要点:

  • JacksonDataFormat 没有构造函数接受一个没有解组类型的 ObjectMapper.但是,在默认构造函数中,当没有提供解组类型时使用 HashMap.class,所以我使用它.通过某种魔法,这似乎习惯于解组所有 POJO 类型.如果您还需要其他类的更具体的数据格式,您也需要在其中设置 ObjectMapper.
  • Camel 似乎会在 bean 注册表中搜索名为json-jackson"的 bean,因此将 Spring bean 设置为使用该名称会使 Camel 不创建新 bean 而是使用我的.
  • 必须将 bean 范围设置为 SCOPE_PROTOTYPE,因为 REST DSL 期望获得 DataFormat 的新实例.请参阅 CAMEL-7880.
  • There is no constructor for JacksonDataFormat that takes an ObjectMapper without an unmarshal type. However, in the default constructor a HashMap.class is used when no unmarshal type is provided, so I use that. By some magic, this appears to then get used to unmarshal all POJO types. If you also need more specific data formats for other classes, you will need to set the ObjectMapper in them too.
  • Camel appears to search the bean registry for a bean called "json-jackson", so setting the Spring bean to use that name tricks Camel into not creating a new one and using mine instead.
  • The bean scope must be set to SCOPE_PROTOTYPE because the REST DSL expects to get a new instance of the DataFormat. See CAMEL-7880.

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

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