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

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

问题描述

我正在尝试使用Jackson在Camel路由上与JSON序列化和反序列化POJO。其中一些有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.

我们只使用Java我们的Spring Boot应用程序的配置,所以没有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.

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

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 而没有unmarshal类型。但是,在默认构造函数中,当没有提供unmarshal类型时使用 HashMap.class ,所以我使用它。通过一些魔术,这似乎习惯于解组所有POJO类型。如果您还需要其他类的更具体的数据格式,您还需要在其中设置 ObjectMapper

  • Camel似乎在bean注册表中搜索一个名为json-jackson的bean,因此设置Spring bean使用该名称会欺骗Camel而不是创建一个新的并使用我的。

  • 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天全站免登陆