Apache Camel 的 REST 端点 [英] REST EndPoint for Apache Camel

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

问题描述

我正在尝试使用 Apache Camel 创建 en REST 端点.我已经有一个返回 JSON 内容的 REST 服务,我希望这个端点能够得到它.我的问题是我不知道我的 Camel 路线建成时发生了什么.. 目前,它没有做任何事情.这是我的代码:

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.

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

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