将POST请求发送到ESB

时间:2014-06-14 05:56:23

标签: wso2 wso2esb google-custom-search synapse

我正在尝试为WSO2 ESB创建Google自定义搜索连接器。创建连接器后,我已将连接器添加到ESB。然后我想测试它。

我的测试初始化​​文件如下:

<template name="listVolume" xmlns="http://ws.apache.org/ns/synapse">
    <parameter name="apiKey" description="Full-text search query string." />
    <parameter name="csiKey" description="Full-text search query string." />
    <parameter name="searchPara" description="Full-text search query string." />
    <sequence>
    <property name="uri.var.apiKey" expression="$func:apiKey" />
    <property name="uri.var.csiKey" expression="$func:csiKey" />
    <property name="uri.var.searchPara" expression="$func:searchPara" />
    <call>
        <endpoint>
            <http method="get"
                  uri-template="https://www.googleapis.com/customsearch/v1?key={uri.var.apiKey}&amp;cx={uri.var.csiKey}&amp;q={uri.var.searchPara}" />
        </endpoint>
    </call>
    </sequence>
</template>

在ESB中配置自定义代理服务后,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="googlecustomsearch_list"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
     <property name="apiKey" expression="json-eval($.apiKey)"/>
     <property name="csiKey" expression="json-eval($.csiKey)"/>
     <property name="searchPara" expression="json-eval($.searchPara)"/>
     <googleCustomSearch.listSearch>
        <apiKey>{$ctx:apiKey}</apiKey>
        <csiKey>{$ctx:csiKey}</csiKey>
        <searchPara>{$ctx:searchPara}</searchPara>
     </googleCustomSearch.listSearch>
     <respond/>
      </inSequence>
      <outSequence>
     <log/>
     <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

然后我在这样的REST客户端中测试它:

POST http://nilash-TECRA-M11:8280/services/googlecustomsearch_list

{
"apiKey":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
 "csiKey":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
 "searchPara":"google"
}

然后我得到这样的输出:

Status Code: 202 Accepted
Connection: keep-alive
Date: Sat, 14 Jun 2014 05:22:34 GMT
Server: WSO2-PassThrough-HTTP
Transfer-Encoding: chunked

但搜索结果不会出现。我在控制台中列出了此错误。

ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.apiKey>. Returning  empty result. Error>>> invalid path
[2014-06-14 10:52:34,883] ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON  Path <$.csiKey>. Returning empty result. Error>>> invalid path
[2014-06-14 10:52:34,884] ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.searchPara>. Returning empty result. Error>>> invalid path

但是,如果我将Google API密钥直接放在我的配置中,就像这样我可以得到预期的结果。

 <http method="get" uri-template="https://www.googleapis.com/customsearch/v1?q={uri.var.searchQuery}&amp;key=XXXXXXXXXXXXXXXXXXX&amp;cx=XXXXXXXXXXXXXXXX"/>

可能是什么问题?

2 个答案:

答案 0 :(得分:3)

您似乎在JSON路径上有错误,请更改

  <googleCustomSearch.listSearch>

 <googleCustomSearch.listVolume>

然后试试。使用下面列出的博客获取更多信息。

http://chanakaindrajith.blogspot.com/2014/04/getting-started-with-wso2-esb-connectors.html

答案 1 :(得分:0)

JSON请求无效。你错过了&#39;,&#39;

&#13;
&#13;
{
"apiKey":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 "csiKey":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 "searchPara":"google"
}
&#13;
&#13;
&#13;