273.5. REST producer 示例

您可以使用其余组件来调用 REST 服务,如任何其他 Camel 组件。

例如,若要使用 hello/{me} 调用 REST 服务,您可以

from("direct:start")
  .to("rest:get:hello/{me}");

然后,动态值 {me} 映射到 Camel 消息,名称相同。因此,要调用此 REST 服务,您可以发送空消息正文和标题,如下所示:

template.sendBodyAndHeader("direct:start", null, "me", "Donald Duck");

Rest producer 需要知道 REST 服务的主机名和端口,您可以按照如下所示配置它们:

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}");

您可以使用 制作者Component 来选择将哪个 Camel 组件用作 HTTP 客户端,例如,可以使用 http4:

restConfiguration().host("myserver:8080/foo").producerComponent("http4");

from("direct:start")
  .to("rest:get:hello/{me}");