2015-06-25 83 views
1

我試圖用Apache Camel創建REST端點。我已經有一個返回JSON內容的REST服務,我希望這個端點能夠得到它。我的問題是,當我的駱駝路線建成時,我不知道發生了什麼。目前,它什麼也沒有做。這裏是我的代碼:REST EndPoint for Apache Camel

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

我在端口9080運行我的REST服務上的本地Tomcat的,我的完整URL是

/ContextServices/REST/contextServices /文件/ {}集合/ {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有什麼區別?

非常感謝!

+0

您是否有任何支持REST的組件?例如camel-servlet – Sergey

+0

嗨,如果這是你的問題,我已經將駱駝servlet加入了pom –

+0

只對pom?你也必須在web.xml中設置servlet(檢查camel.apache.org/servlet.html) – Sergey

回答

1

首先,REST組件本身不是REST實現。 它只是聲明語言來描述REST端點。 你應該用實際執行的休息時間,像的Restlet(查看完整列表here

我可能是錯的,但是當你想監聽從其他應用程序REST請求AFAIR,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組件的方式重寫它。

+0

嗨,謝謝你的回答,我用restlet替換它,並且它工作的很完美。對於REST組件,我還沒有理解這樣的事情! –