41.7. REST producer 示例
您可以使用其余组件调用 REST 服务,如任何其他 Camel 组件。
例如,要使用 hello/{me} 在 上调用 REST 服务,您可以执行以下操作
from("direct:start")
.to("rest:get:hello/{me}");
from("direct:start")
.to("rest:get:hello/{me}");
然后,动态值 {me} 映射到具有相同名称的 Camel 消息。为了调用此 REST 服务,您可以发送空消息正文和标头,如下所示:
template.sendBodyAndHeader("direct:start", null, "me", "Donald Duck");
template.sendBodyAndHeader("direct:start", null, "me", "Donald Duck");
Rest producer 需要知道 REST 服务的主机名和端口,您可以使用 host 选项进行配置,如下所示:
from("direct:start")
.to("rest:get:hello/{me}?host=myserver:8080/foo");
from("direct:start")
.to("rest:get:hello/{me}?host=myserver:8080/foo");
您可以在 restConfiguration 中配置主机,而不是使用 host 选项,如下所示:
restConfiguration().host("myserver:8080/foo");
from("direct:start")
.to("rest:get:hello/{me}");
restConfiguration().host("myserver:8080/foo");
from("direct:start")
.to("rest:get:hello/{me}");
您可以使用 producerComponent 选择将哪个 Camel 组件用作 HTTP 客户端,例如要使用 http :
restConfiguration().host("myserver:8080/foo").producerComponent("http");
from("direct:start")
.to("rest:get:hello/{me}");
restConfiguration().host("myserver:8080/foo").producerComponent("http");
from("direct:start")
.to("rest:get:hello/{me}");