REST EndPoint for Apache Camel

2019-08-08 02:18发布

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

I'm running my REST service on a local Tomcat on port 9080, my full URL is

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

or

rest("/say")
    .get("/hello").to("direct:hello")
    .get("/bye").consumes("application/json").to("direct:bye")
    .post("/bye").to("mock:update");

The first is Java DSL, the second is REST DSL, what's the difference ?

Thanks a lot !

1条回答
混吃等死
2楼-- · 2019-08-08 02:28

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)

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>

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.

查看更多
登录 后发表回答