【问题标题】:REST EndPoint for Apache CamelApache Camel 的 REST 端点
【发布时间】:2015-06-25 09:27:14
【问题描述】:

我正在尝试使用 Apache Camel 创建一个 REST 端点。我已经有一个返回 JSON 内容的 REST 服务,我希望这个端点能够获取它。我的问题是,当我的骆驼路线建成时,我不知道发生了什么。目前,它什么也没做。这是我的代码:

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());

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

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

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

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,有什么区别?

非常感谢!

【问题讨论】:

  • 您有任何支持 REST 的组件吗?例如骆驼servlet
  • 您好,如果这是您的问题,我已将 camel-servlet 添加到 pom 中
  • 只对pom?您还必须在 web.xml 中设置 servlet(检查 camel.apache.org/servlet.html)

标签: java rest apache-camel


【解决方案1】:

首先,REST 组件本身并不是一个 REST 实现。 它只是声明了描述 REST 端点的语言。 您应该使用 REST 的实际实现,例如 Restlet(请参阅完整列表 here

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

<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 组件之类的东西重写它。

【讨论】:

  • 您好,感谢您的回答,我将其替换为restlet,它可以完美运行。对于 REST 组件,我不明白这样的事情!
猜你喜欢
  • 2020-02-25
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多