Camel JAXB 封送返回 XML 对象类型



我正在将 Java 对象发送到生产者端点并等待编组的 XML 对象。我尝试将其更改为Node对象/文件对象,但它给出了ClassCastException.

所以在对象类类型中采用了 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>

unmarshall处理器返回流,而不是单个对象。在骆驼中,更一般地说,如果你想要一个特定的类型,你不应该直接将一个 body 作为对象,而是使用各种方法将 body 转换为该类型。

尝试:

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

最新更新