【问题标题】:Issue with Camel custom @Converter using Spring DSL使用 Spring DSL 的 Camel 自定义 @Converter 问题
【发布时间】:2017-05-26 15:25:48
【问题描述】:

我只是想学习自定义转换器并遇到问题。任何帮助深表感谢。 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

【问题讨论】:

    标签: apache-camel spring-dsl


    【解决方案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 组件。

    【讨论】:

    • 运气不好。更新更改如下。能否请您帮忙 Converter public class MyConvertor { Converter public JSONObject convertTo(GenericFile file) throws JSONException, IOException { return jsonObject; } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2018-03-03
    • 1970-01-01
    相关资源
    最近更新 更多