gpt4 book ai didi

How to use Camel convertBodyTo with a list(如何将Camel ConvertBodyTo与列表一起使用)

转载 作者:bug小助手 更新时间:2023-10-25 22:49:20 27 4
gpt4 key购买 nike



I have a list of type A and I would like to transform it in a list of type B.

我有一个A类型的列表,我想把它转换成B类型的列表。


Let's imagine:

让我们想象一下:


// ... call the db and get a few rows mapped to List<A>
.to("jdbc:...")
.convertBodyTo(List.class) // ?!?

In Camel, if it was a "normal" type, I would just use: .convertBodyTo(B.class), then let Camel find the converter I coded.

在Camel中,如果它是“普通”类型,我只需要使用:.ConvertBodyTo(B.class),然后让Camel找到我编写的转换器。


However, since this is a list of type A, if I do .convertBodyTo(List.class), the converter is just not called.

但是,因为这是一个A类型的列表,所以如果我调用.ConvertBodyTo(List.class),转换器就不会被调用。


I could not split and process one at the time, because I need to do some grouping on List<A> before converting.

我当时无法拆分和处理一个,因为我需要在转换之前对列表进行一些分组。


I could fix it by using a bean, but I wanted to know if there is any way to call convertBodyTo to convert from a list to a list.

我可以通过使用Bean来修复它,但我想知道是否有任何方法可以调用ConvertBodyTo将列表转换为列表。


更多回答
优秀答案推荐

You can look into how to write your own Type Converters here, but I don't believe they will work in your case due to Java's type erasure. As you alluded to, you'll just be converting a List to a List.

您可以在这里研究如何编写您自己的类型转换器,但我不相信它们会在您的情况下工作,因为Java的类型擦除。正如您提到的,您只是将一个列表转换为一个列表。


Personally I'd probably just implement a Processor and call .process(..). It's a bit neater than .bean(..) for this use case.

就我个人而言,我可能只会实现一个处理器并调用.process(..)。它比.Bean(..)更整洁一些。用于此用例。


更多回答

IMHO a transformer (camel.apache.org/components/4.0.x/eips/transform-eip.html) would be even better (more meaningful) than a simple Processor. My 2 cents

我认为一个转换器(camel.apache.org/components/4.0.x/eips/transform-eip.html)会比一个简单的处理器更好(更有意义)。我的两分钱

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com