2014-01-09 91 views
0

我想發送一個http post到外部web服務,我需要調用。apache camel - 調用外部web服務

<from uri="cxfrs://http://localhost:9876?resourceClasses=MyResource"/> 
      <log message="Received. " loggingLevel="INFO" logName="MyLogger"/> 
      <setHeader headerName="CamelHttpMethod"> 
       <constant>POST</constant> 
      </setHeader> 
      <setHeader headerName="Content-Type"> 
       <constant>application/json</constant> 
      </setHeader> 
      <setBody> 
       <simple>param1=param1value&amp;param2=param2value</simple> 
      </setBody> 
<to uri="http://samplesample.com?bridgeEndpoint=true" /> 
<log message="body is ${body}" loggingLevel="INFO" logName="MyLogger"/> 

我得到一個異常:

Caused by: org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking http://samplesample.com with statusCode: 400 

當我啓動使用其他客戶端的請求,它工作正常。任何幫助將不勝感激。

+0

那麼根據錯誤你是不是發送正確的語法。下載wireshark對其他客戶端調用和駱駝呼叫進行數據包嗅探並進行比較。我相信這兩個電話是不同的,你需要找出原因。 – Namphibian

回答

2

camel-cxfrs消費者會將REST請求轉變爲方法調用,因此駱駝http生產者可能不會正確解釋消息。

如果您想通過使用camel代理REST請求,那麼您可以使用camel-jetty組件來執行此操作。

from("jetty://http://localhost:9876?matchOnUriPrefix=true") 
        .to("http://samplesample.com?throwExceptionOnFailure=false&bridgeEndpoint=true"); 
0

而不是將您的請求參數放到郵件正文中,您應該設置適當的標題,例如,添加HTTP查詢參數使用

<setHeader headerName="CamelHttpQuery"> 
<constant>param1=param1value&amp;param2=param2value</constant> 
</setHeader> 

或添加HTTP路徑參數使用

<setHeader headerName="CamelHttpPath"> 
<constant>/param1/20</constant> 
</setHeader>