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(..)更整洁一些。用于此用例。
更多回答
我是一名优秀的程序员,十分优秀!