0

我只想学习自定义转换器并遇到问题。任何帮助深表感谢。Camel 2.17 版和 JBoss Fuse 6.3

@Converter
public class MyConvertor{

    public MyConvertor(){}

    @Converter
    public static String convertTo(Exchange exchange) {}

}

在我的 Spring DSL 中

<convertBodyTo charset="UTF-8" id="_convertBodyTo1" type="com.x.convertor.MyConvertor"/>

在 META-INF/services/org/apache/camel/TypeConverter

com.x.convertor.MyConvertor

错误信息 :

 org.apache.camel.InvalidPayloadException: No body available of type: com.x.convertor.MyConvertor but has value: GenericFile[output.txt] of type: org.apache.camel.component.file.GenericFile on: output.txt. Caused by: No type converter available to convert from type: 
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.apache.camel.component.file.GenericFile to the required type: com.x.convertor.MyConvertor
4

1 回答 1

1

有几个错误。该type属性应该是您的目标类型(转换后您想要的类型)。

<convertBodyTo charset="UTF-8" id="_convertBodyTo1" type="java.lang.String"/>

Camel 可以自动进行这种转换。如果您想自己编写转换器,请注意转换器方法应该将预期的输入类型作为参数,而不是 Exchange(从 Camel 2.16 开始,它可以是可选的第二个参数)。
类应该是这样的:

@Converter
public class MyConvertor{

    public MyConvertor(){}

    @Converter
    public static String convertTo(GenericFile body, Exchange exchange) {
        // The exchange parameter is optional
    }

}

请参阅https://camel.apache.org/type-converter.html
如果要读取 CSV 文件的内容以将其转换为 POJO,请使用Bindy组件。

于 2017-05-26T07:25:48.630 回答