Apache Camel 的 REST 端点
REST EndPoint for Apache Camel
我正在尝试使用 Apache Camel 创建 en REST 端点。我已经有一个 return 我 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());
我 运行 我的 REST 服务在端口 9080
上的本地 Tomcat 上,我的完整 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组件本身并不是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 组件之类的东西重写它。
我正在尝试使用 Apache Camel 创建 en REST 端点。我已经有一个 return 我 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());
我 运行 我的 REST 服务在端口 9080
上的本地 Tomcat 上,我的完整 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组件本身并不是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 组件之类的东西重写它。