繁体   English   中英

使用Apache Camel从对象生成XML文件并将其保存到文件中

[英]Generating XML file from an object and saving it to a file on the fly with Apache Camel

如标题所示,我想从一个类生成XML文件,然后使用Apache Camel将其复制到某个路径。

我可以创建一个临时文件并使用Camel复制该临时文件,但是我想即时执行此操作,例如将对象交给Camel并编组并复制到我想要的位置。

首先,我班上的代码看起来像这样:

@XmlRootElement
@XmlType(propOrder = {"name"})
public class Person
{
   private String name;  
   public Person()
   {

   }

   public Person(String name)
   {
      this.name = name;
   }

   @XmlAttribute
   public String getName()
   {
      return this.name;
   }
}

我尝试了两种方法:

  • 将类转换为XML字符串并将其提供给Camel。
  • 将物体直接喂入骆驼。

不幸的是,我在Camel中找不到任何字符串端点,因此以下代码无法正常工作:

Person person = new Person("test name");
String personString;
StringWriter sw = new StringWriter();

JAXBContext personContext = JAXBContext.newInstance(Person.class);
Marshaller personMarshaller = personContext.createMarshaller();
personMarshaller.marshal(person, sw);
personString = sw.toString();

from(personString)
.routeId("TestRoute")
.to("file:/apps/test/test.xml");

因此,我的下一个测试是将对象直接提供给Camel,看它是否具有类终结点:

Person person = new Person("test name");

from("class:" + person)
.routeId("TestRoute")
.marshal().jaxb(Person.class.getPackage().getName().toString())
.to("file:/apps/test/test.xml");

该代码失败,但有以下异常:

Failed to create route TestRoute: Route(TestRoute)[[From[class:com.test.camel... because of Failed to resolve endpoint: class://com.test.camel.Person@21912909 due to: com.test.camel.Person@21912909: org.apache.camel.FailedToCreateRouteException: Failed to create route TestRoute: Route(TestRoute)[[From[class:com.test.camel... because of Failed to resolve endpoint: class://com.test.camel.Person@21912909 due to: com.test.camel.Person@21912909

那么我该怎么做呢? 这个错误是什么意思? 任何帮助,将不胜感激。

使用直接组件: http : //camel.apache.org/direct 因此,您的路由从直接开始,然后使用ProducerTemplate从Java代码中调用该路由。

请参阅入门指南http://camel.apache.org/getting-started.html http://camel.apache.org/walk-through-an-example.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM