2016-11-07 128 views
2

我想在骆驼蓝图中调用POST rest服务。我的蓝图XML文件如下:如何在蓝图中使用Camel调用REST调用(POST with JSON body)

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
xmlns:camel="http://camel.apache.org/schema/blueprint" 
xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" 
xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="  http://www.osgi.org/xmlns/blueprint/v1.0.0  https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd  http://camel.apache.org/schema/blueprint  http://camel.apache.org/schema/blueprint/camel-blueprint.xsd  http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> 
<bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider" id="jsonProvider"/> 
<cxf:rsClient address="http://jsonplaceholder.typicode.com/posts" 
    id="rsClient" loggingFeatureEnabled="true"> 
    <cxf:providers> 
     <ref bean="jsonProvider" component-id="jsonProvider"/> 
    </cxf:providers> 
</cxf:rsClient> 
<camelContext id="RESTCamelContext" xmlns="http://camel.apache.org/schema/blueprint"> 
    <route id="RESTRoute"> 
     <from id="_from1" uri="timer:foor?repeatCount=1"/> 
     <to id="_to1" uri="log:body?level=INFO"/> 
     <setHeader headerName="Content-Type" id="_setHeader1"> 
      <constant>application/json</constant> 
     </setHeader> 
     <setHeader headerName="Exchange.HTTP_METHOD" id="_setHeader2"> 
      <constant>POST</constant> 
     </setHeader> 

     <to id="_to2" uri="cxfrs:bean:rsClient"/> 
     <to id="_to3" uri="log:body?level=INFO"/> 
    </route> 
</camelContext> 

,但我不知道如何通过JSON对象体内请求。

回答

2

最简单的方法是使用HTTP4 component

<setHeader headerName="CamelHttpMethod"> 
    <constant>POST</constant> 
</setHeader> 
<setBody> make your POJO here </setBody> 
<marshal> 
    <json library="Jackson" /> 
</marshal> 
<to uri="http4://jsonplaceholder.typicode.com/posts"/> 
+0

如何设置我的POJO类的JSON对象发布? – chris

+0

只需将身体设置为您的POJO对象,并使用您的首选库添加“marshal”步骤将其转换为JSON –

+0

谢谢。你可以告诉我这是如何在代码中翻译的吗? – chris