如何通过Apache Camel调用RESTful服务?

我目前正在使用HTTP方法来调用一些会产生JIRA问题的URL。

现在我想使用Apache Camel,我该如何使用它?

我需要通过Camel调用以下链接:

http://localhost:8080/rest/api/2/project/" + key + /components 

由于我是Camel的新手,请提供一些解决方案和示例。

谢谢

您可以轻松使用CXFRS组件 ; 如果由于某种原因需要使用HTTP组件 ,您也可以轻松使用它:

  http://localhost:8080/rest/api/2/project/${header.myKey}/components   

当然,在进入路径的这一部分之前,您需要使用myKey标头来丰富您的消息。

另请参阅此常见问题解答,了解如何在Camel中使用动态到端点http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

基本上,EIP模式就是收件人列表。

因此,在您的情况下,它也可以简化为一个EIP

  http://localhost:8080/rest/api/2/project/${header.myKey}/components  

请注意,Camel中的http组件是完全同步的。 如果您想通过HTTP进行请求/回复并避免在等待回复消息时使用调用程序块,那么您可以使用Camel中的一些其他HTTP组件,例如:

  • 骆驼AHC
  • 骆驼http4
  • 骆驼docker

我正在使用apache camel jetty

 CamelContext context = new DefaultCamelContext(); public void configure(){ context.addRoutes(new RouteBuilder(){ from("jetty:localhost:9000/offers") .to("direct:getOffers") .end(); } }); 

所以当用户点击http:// localhost:9000 / offers时 ,端点直接:getOffers将被调用

所以现在定义getOffers端点

 context.addRoutes(new RouteBuilder(){ public void configure(){ from("direct:getOffers") .to("jetty:http://localhost:9008/api/v2.0/offers? bridgeEndpoint=true") .end(); } }); 

这里有另一个服务在9008运行,其rest资源为http:// localhost:9008 / api / v2.0 / offers ,这是我尝试使用的资源。

因此,当camel实例启动时,它会注册两个路由,然后执行上述处理

注意为此工作添加?bridgeEndpoint = true选项很重要

您可以使用CXFRS Component从camel使用REST服务.Apache camel有足够的信息。

http://camel.apache.org/cxfrs.html