2016-02-05 38 views
0

我正在向生產者端點發送Java對象並等待編組的XML對象。我試圖將其更改爲Node對象/文件對象,但它給出ClassCastException駱駝JAXB編組返回XML對象類型?

所以把xmlObj放在一個對象類類型中。什麼應該是正確的類來捕捉響應?

public class ClientEight { 

    @Produce(uri = "direct:invoice") 
    ProducerTemplate template; 

    public static void main(String args[]) throws InterruptedException { 
     AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("resources/camel-configTen.xml"); 
     InvoiceXml invoice = new InvoiceXml("fdf3443", 3454, 435345.44 f, "hfhfddfdg"); //any java object we are passing 
     ClientEight client = (ClientEight) ctx.getBean("client"); 
     Object xmlObj = client.template.requestBody(invoice); 
     System.out.println(xmlObj); 
    } 
} 

以上就是您所使用的Java對象發送到生產者端點,因爲你正在使用template.requestBody,你得到返回的對象返回客戶端代碼。

<camel:camelContext> 
     <camel:dataFormats> 
      <!-- path to jaxb annotated class --> 
      <camel:jaxb id="invoiceJaxb" contextPath="com.java.bean" 
       prettyPrint="true" /> 
     </camel:dataFormats> 
     <camel:route> 
      <camel:from uri="direct:invoice" /> 
      <camel:marshal ref="invoiceJaxb" /> 
      <camel:log message=" ${body}" /> 
      <camel:to uri="file://src/resources?fileName=One.xml"/> 
     </camel:route> 
    </camel:camelContext> 

回答

0

unmarshall處理器返回一個流,而不是一個單一的對象。在駱駝中,更普遍的是,如果你想要一個特定的類型,你不應該直接把身體作爲一個對象,而是使用各種方法將身體轉換爲這種類型。

嘗試:

Document document = client.template.requestBody(invoice, org.w3c.dom.Document.class); 
+0

會如何,我們將它轉​​換爲XML類型? –