2017-05-25 131 views
0

我只想學習自定義轉換器並遇到問題。任何幫助深表感謝。駱駝版本2.17和JBoss保險絲6.3問題與駱駝定製@Converter使用春季DSL

@Converter 
public class MyConvertor{ 

    public MyConvertor(){} 

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

} 

在我的春天DSL

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

在META-INF /服務/組織/阿帕奇/駱駝/ 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 

回答

0

有幾個呃RORS。 type屬性應該是您的目標類型(轉換後需要的類型)。

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

駱駝可以自動做這種轉換。如果您想自己編寫一個轉換器,請注意,轉換器方法應該具有預期的輸入類型作爲參數,而不是Exchange(可以是駝峯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組件。

+0

沒有運氣。更新如下。你能幫 轉換 公共類MyConvertor { \t轉換 \t公衆的JSONObject的ConvertTo(GenericFile 文件)拋出JSONException,IOException異常{ \t \t回報的JSONObject; \t} }