Apache camel 将XML信息转换为Java-ApacheCamel

Apache camel 将XML信息转换为Java-ApacheCamel,apache-camel,Apache Camel,这是与apache camel二进制文件捆绑在一起的示例 <route> <!-- incoming requests from the servlet is routed --> <from uri="servlet:///hello"/> <choice> <when> <!-- is there a header with the key name?

这是与apache camel二进制文件捆绑在一起的示例

 <route>
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:///hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hello ${header.name} how are you?</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>
但我不知道如何继续…

试试这个:

from("servlet://hello")
.choice()
.when(header("name").isNotNull()).transform(simple("Hello ${header.name} how are you?"))
.otherwise().transform(constant("Add a name parameter to uri, eg ?name=foo"));
试试这个:

from("servlet://hello")
.choice()
.when(header("name").isNotNull()).transform(simple("Hello ${header.name} how are you?"))
.otherwise().transform(constant("Add a name parameter to uri, eg ?name=foo"));

如果您想在不使用任何XML(即spring)的情况下将其移植到java,则不能(轻松地)使用servlet组件

只需移植该路由就可以如下所示:

from("servlet:///hello")
      .choice()
        .when()
           .header("name")
              .transform(simple("Hello ${header.name} how are you?"))
        .otherwise()
            .transform(constant("Add a name parameter to uri, eg ?name=foo"));
它应该在spring示例(或任何SpringWeb应用程序)中工作,只要将
中的
替换为
,因为您已经将路由定义为Springbean(

但是,我想您希望在纯java中实现这一点(没有spring、xml、webapp)。你可以使用码头组件。区别在于Camel随后将启动servlet容器,而不是servlet容器启动Camel。不过,这个简单的例子没有什么不同

我建议你从一个Maven原型开始,把骨架弄起来

e、 g.
mvn原型:生成
然后选择
org.apache.camel.archetypes:camel原型java(使用JavaDSL创建一个新的camel项目)。
好吧,如果您有自己的java应用程序并且线程一直在运行,那么就不需要maven原型。那么你的方法应该很好。然而,maven原型对于训练来说是非常好的

然后需要向Jetty(camel Jetty.jar)添加依赖项(阅读更多)

除了第一行:
from(“码头:http://localhost:8080/camel/hello“”


非常简单。

如果您想在不使用任何XML(即spring)的情况下将其移植到java,则不能(轻松地)使用servlet组件

只需移植该路由就可以如下所示:

from("servlet:///hello")
      .choice()
        .when()
           .header("name")
              .transform(simple("Hello ${header.name} how are you?"))
        .otherwise()
            .transform(constant("Add a name parameter to uri, eg ?name=foo"));
它应该在spring示例(或任何SpringWeb应用程序)中工作,只要将
中的
替换为
,因为您已经将路由定义为Springbean(

但是,我想您希望在纯java中实现这一点(没有spring、xml、webapp)。你可以使用码头组件。区别在于Camel随后将启动servlet容器,而不是servlet容器启动Camel。不过,这个简单的例子没有什么不同

我建议你从一个Maven原型开始,把骨架弄起来

e、 g.
mvn原型:生成
然后选择
org.apache.camel.archetypes:camel原型java(使用JavaDSL创建一个新的camel项目)。
好吧,如果您有自己的java应用程序并且线程一直在运行,那么就不需要maven原型。那么你的方法应该很好。然而,maven原型对于训练来说是非常好的

然后需要向Jetty(camel Jetty.jar)添加依赖项(阅读更多)

除了第一行:
from(“码头:http://localhost:8080/camel/hello“”


又好又简单。

挺直的。谢谢此示例支持“GET”。我如何做一个“POST”?如果使用GET或POST(即使在示例中)实际上并不重要。这也适用于该示例(使用瑞典制造的HTTP工具cURL)
cURL-X POST-d name=superherohttp://localhost:8080/camel/hello
即使是多数据表单帖子也能很好地工作。看看这些文件,很直接。谢谢此示例支持“GET”。我如何做一个“POST”?如果使用GET或POST(即使在示例中)实际上并不重要。这也适用于该示例(使用瑞典制造的HTTP工具cURL)
cURL-X POST-d name=superherohttp://localhost:8080/camel/hello
即使是多数据表单帖子也能很好地工作。查看文档。