REST EndPoint for Apache骆驼 [英] REST EndPoint for Apache Camel

查看:103
本文介绍了REST EndPoint for Apache骆驼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache Camel创建REST端点.我已经有一个REST服务,可以将JSON内容返回给我,并且我希望此端点能够获取它.我的问题是,我不知道构建骆驼路线时会发生什么情况..目前,它什么也没做.这是我的代码:

I'm trying to create en REST endpoint with Apache Camel. I already have a REST service that return me JSON content and I want this endpoint to get it. My problem is that I don't know what's happening when my Camel route is built.. For the moment, it doesn't do anything. Here is my code :

restConfiguration().component("servlet")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true").host("localhost")
.port(9080);    

rest("/ContextServices/rest/contextServices/document")
.consumes("application/json").produces("application/json")
.get("/testContext/557064c8f7f71f29cea0e657").outTypeList(String.class)
.to("bean:processor?method=affiche")
.to(dest.getRouteTo());

我正在端口9080上的本地Tomcat上运行我的REST服务,我的完整URL是

I'm running my REST service on a local Tomcat on port 9080, my full URL is

/ContextServices/rest/contextServices/document/{collection}/{id}.

/ContextServices/rest/contextServices/document/{collection}/{id}.

我尝试阅读文档,但是有两种语法,但是两种语法都不起作用:

I've tried to read the documentation but there is two syntax and both don't work:

from("rest:get:hello:/french/{me}").transform().simple("Bonjour ${header.me}");

rest("/say")
    .get("/hello").to("direct:hello")
    .get("/bye").consumes("application/json").to("direct:bye")
    .post("/bye").to("mock:update");

第一个是Java DSL,第二个是REST DSL,有什么区别?

The first is Java DSL, the second is REST DSL, what's the difference ?

非常感谢!

推荐答案

首先,REST组件本身不是REST实现. 它只是声明描述REST端点的语言. 您应该使用REST的实际实现,例如Restlet(请参见完整列表此处)

First of all, REST component itself is not a REST implementation. It just declares language to describe REST endpoints. You should use actual implementation of REST, something like Restlet (see the full list here)

我可能是错的,但是AFAIR,REST端点仅在您要侦听来自其他应用程序的REST请求时才适用. 您需要向REST端点发出请求并进行处理. 问题是:您什么时候要触发请求? 是某个事件,还是您想定期检查外部REST服务? 对于后一种情况,我使用以下模式:

I can be wrong, but AFAIR, REST endpoint is only for the case when you want to listen for REST requests from another application. What you need is to make request to REST endpoint and process it. The question is: when do you want to trigger request? Is it some event, or may be you want to check external REST service periodically? For the latter case I use the following pattern:

<route>
  <from uri="timer:polling-rest?period=60000"/>
  <setHeader headerName="CamelHttpMethod">
    <constant>GET</constant>
  </setHeader>
  <recipientList>
    <simple>http://${properties:service.url}/api/outbound-messages/all</simple>
  </recipientList>
  <unmarshal ref="message-format"/>

  <!-- do something with the message, transform it, log,
       send to another services, etc -->

  <setHeader headerName="CamelHttpMethod">
    <constant>DELETE</constant>
  </setHeader>
  <recipientList>
    <simple>http://${properties:service.url}/api/outbound-messages/by-id/${header.id}</simple>
  </recipientList>
</route>

很抱歉,使用http组件而不是REST的示例. 我只是从使用纯http的工作项目中复制粘贴了它. 我想,可以通过Restlet或CXF组件将其重写.

Sorry for the example with http component instead of REST. I simply copy-pasted it from my working project, which uses pure http. I suppose, rewriting this via something like Restlet or CXF component.

这篇关于REST EndPoint for Apache骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆