首页 > 解决方案 > 在骆驼中连续调用3个restful api

问题描述

我有一个 Web 服务调用名为 API1 的 API,那么如果 API1 的响应是{"result":"0"}第二个 API,API2 将被调用。API2 用于上传文件,如果上传成功,将调用第三个 API,API3。我的问题是消息正文没有正确发送到 API3 没有任何错误!

我的代码如下:首先我发送一个包含以下内容的发布请求:

{
    "API1": {
        "version": "Tooba_Uni_Appv0.4.1.apk"
    },
    "API2": {
        "password": "password",
        "username": "username",
        "file": "/home/.../index.png"
    },
    "API3": {
        "lastVer": "apk.1.10.0"
    }
}

setMainBody 进程将消息的内容设置为上面提到的init 内容。

<camelContext id="camel-CallAPI" xmlns="http://camel.apache.org/schema/blueprint" >
    <!-- Web service starts working -->
    <restConfiguration component="restlet" host="192.168.100.232" port="8889"/>
    <rest path="/say">
        <post uri="/hi" consumes="application/json" produces="application/json">
            <to uri="direct:start"/>
        </post> 
    </rest>  

    <route>         
        <from uri="direct:start"/>
            <convertBodyTo type="java.lang.String"/>
            <setProperty propertyName="mainBody">
                <simple>${body}</simple>
            </setProperty>
            <removeHeaders pattern="*" excludePattern="Content-Type|CamelHttpMethod"/>
            <setHeader headerName="CONTENT_TYPE" >
                <constant>application/json</constant>
            </setHeader>
            <process ref="getVersionParam"/>
            <convertBodyTo type="java.lang.String"/>
            <log message="before api1 and after process :: ${body}"/> 
            <to uri="direct:first"/>               
    </route>  

    <route>
        <from uri="direct:first"/>    
            <to uri="http4://...path to API1"/>
            <convertBodyTo type="java.lang.String"/>
            <log message="after api1 :: ${body}"/> 
            <log message="if the result is 0 the Upload API should be called, otherwise the result should be stored in queue!"/>
            <!-- <setProperty propertyName="BodyAfterAPI1">
                     <simple>${body}</simple>
                 </setProperty> -->
            <setProperty propertyName="AccessResultAPI1">
                <jsonpath>$.result</jsonpath>
            </setProperty>
            <choice>
                <when>
                    <!-- the testCheckLastVersion API does not work fine and The result of API will be stored in the queue-->
                    <simple>${property.AccessResultAPI1} != '0'</simple>
                    <inOnly id="ErrorQueueTestVersion" uri="activemq:queue:ErrorQueueTestVersionAPI"/> 
                    <log message="the testCheckLastVersion API has a problem and the Upload API can not be called."/>
                </when>
                <when>
                    <!-- the testCheckLastVersion API works fine and Upload API must be called-->
                    <simple>${property.AccessResultAPI1} == '0'</simple>
                    <log message="the testCheckLastVersion work fine  and the Upload API will be call soon,${property.AccessResultAPI1}"/>
                    <to uri="direct:callUploadAPI" />
                </when>
            </choice> 
    </route>

    <route id="route_callUploadAPI">
        <from uri="direct:callUploadAPI" id="from_callUploadAPI"/>
            <process ref="setMainBody"/>
            <convertBodyTo type="java.lang.String"/>
            <log message="pure body: ${body}"/>
            <removeHeaders pattern="*" excludePattern="Content-Type|CamelHttpMethod"/>
            <setHeader headerName="CONTENT_TYPE" >
                <constant>multipart/form-data</constant>
            </setHeader>
            <process ref="getUploadParam"/>
            <to uri="http4://...path to upload API2"/>
            <convertBodyTo type="java.lang.String"/>
            <log message="the Upload API: ${body} :: if the success was 1 the third API will be called "/>
            <setProperty propertyName="AccessResultAPI2">
                <jsonpath>$.success</jsonpath>
            </setProperty>
            <choice>
                <when>
                <!--when the Upload API does not works fine and the result of Upload API must be stored in the queue-->
                    <simple>${property.AccessResultAPI2} != '1'</simple>
                    <inOnly id="ErrorQueueAPIUpload" uri="activemq:queue:ErrorQueueUploadAPI"/> 
                    <log message="the Upload API has a problem and the TestsetVersion API can not be called."/>
                </when>
                <!--when the Upload API works fine and TestsetLastVersion API must be called-->
                <when>
                    <simple>${property.AccessResultAPI2} == '1'</simple>
                    <log message="the Upload API works, success: ${property.AccessResultAPI2}"/>
                    <to uri="direct:midle"/>
                </when>
            </choice>
    </route>

    <route>         
        <from uri="direct:midle"/>
            <!-- <process ref="setMainBody"/>
                 <convertBodyTo type="java.lang.String"/>-->
            <log message="body aftre setMainbody: ${body}"/> 
            <removeHeaders pattern="*" excludePattern="Content-Type|CamelHttpMethod"/>
            <setHeader headerName="CamelHttpMethod" >
                <constant>POST</constant>
            </setHeader>
            <setHeader headerName="CONTENT_TYPE" >
                <constant>application/json</constant>
            </setHeader>
            <!-- <process ref="getLastVersionParam"/> 
                 <convertBodyTo type="java.lang.String"/>-->
            <setBody>
                <constant>{"lastVer":"apk.1.10.0"}</constant>
            </setBody>
            <convertBodyTo type="java.lang.String"/>
            <to uri="http4://...path to API3"/> 
            <convertBodyTo type="java.lang.String"/> 
            <log message="the testsetLastVersion API: ${body}"/>                
            <log message="The TestsetLastversion API is ok and the process finished successfuly."/>
    </route>

</camelContext>

问题是,body 的内容没有发送到第三个 API,也没有返回响应!

标签: jsonrestapiapache-camelpostman

解决方案


推荐阅读