骆驼路线逻辑/输出预期

时间:2016-02-04 21:49:17

标签: java jaxb apache-camel marshalling unmarshalling

背景: 我试图使用jaxb和camel解组xml文件。我觉得我很难用它,因为我不确切地知道文件的内容。

例如,我有:

from("file://C:/test.xml").unmarshal(jaxb).to("file://C:/testEnd.java");

有了这个,我希望在.java文件中看到解组的结果(即xml文件元素中的参数和值)。但是,当我运行程序时,.java文件中没有任何内容,但我没有收到任何错误。

编组时也会发生同样的事情。当我有一个.java文件作为from函数和一个.xml文件在to函数中时,没有任何反应。

例如,我有:

from("file://C:/test.java").marshal(jaxb).to("file://C:/testEnd.xml");

从此,我希望看到我的带注释的java文件中的值出现在xml文件中。

问题: 我对这两种情况的期望是否正确?或者这个逻辑有什么问题吗?

2 个答案:

答案 0 :(得分:1)

请试试这个: 如果在你的代码中你想以xml的形式保存java对象,然后再次使用该xml来检索之前保存的java对象的状态,我们会进行编组和解组 1)编组:将java对象转换为基于xml并将其保存到文件中 创建一个producerTemplate,将java对象发送到producerendpoint,根据jaxb dataformat封送它,然后使用标记为XmlRootElement的pojo bean将其转换为xml,并在jaxb标记中称为contextPath。

public class ClientEight {

    @Produce(uri="direct:invoice")
    ProducerTemplate template;
public static void main(String rgs[]) throws InterruptedException{
AbstractApplicationContext ctx= new ClassPathXmlApplicationContext("resources/camel-configTen.xml");
        InvoiceXml invoice= new InvoiceXml("fdf3443",3454, 435345.44f, "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>

这将是你的驼峰配置文件。希望这有帮助

答案 1 :(得分:0)

file组件获取目录,并处理此目录中的所有文件。它默认不处理文件,你必须使用选项或流组件。

请参阅http://camel.apache.org/file2.html

  

仅限目录

     

Camel仅支持使用a配置的端点   起始目录。所以directoryName必须是一个目录。如果你   想要只使用一个文件,可以使用fileName选项,   例如通过设置fileName = thefilename。另外,起始目录   不得包含$ {}占位符的动态表达式。再次使用   fileName选项,用于指定文件名的动态部分。