覆盖 Spring Integration dsl 使用的 fasterxml jackson ObjectMapper

Override fasterxml jackson ObjectMapper used by Spring Integration dsl

使用 Transformers.toJson() 我的 json 日期如下所示:

"createdDate":{"year":2017,"month":"OCTOBER","monthValue":10,"dayOfMonth":25,"hour":7,"minute":57,"second":36,"nano":972000000,"dayOfWeek":"WEDNESDAY","dayOfYear":298, "chronology":{"calendarType":"iso8601","id":"ISO"}}

这是出站 ampq 配置:

@Bean
public IntegrationFlow outboundCdrRabbitFlowDefinition() {
    return IntegrationFlows.from(CHANNEL_NAME)
            .transform(Transformers.toJson())
            .handle(Amqp.outboundAdapter(new RabbitTemplate(cachingConnectionFactory))
                    .routingKey("routing-key"))
            .get();
}

兔子队列的消费者期望格式"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

有什么方法可以覆盖 spring 集成使用的默认 ObjectMapper?

例如,我在我的 Web api 配置中有这个扩展 WebMvcConfigurerAdapter 的配置:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setTimeZone(UTC_TIME_ZONE);
    mapper.setDateFormat(ISO_8601_DATE_FORMAT);
    mapper.registerModule(new Jdk8Module());
    mapper.registerModule(new JavaTimeModule());
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper());
    return mappingJackson2HttpMessageConverter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(mappingJackson2HttpMessageConverter());
    super.configureMessageConverters(converters);
}

有没有什么方法可以在 Spring 集成中重用 objectMapper bean,以便映射配置在我所有的出站端点、web api http 或集成 ampq 中都是相同的?

存在 Transformers.toJson() 的重载版本:

Transformers.toJson(new Jackson2JsonObjectMapper(objectMapper))