ESB WSO2-在数据源查询中使用数组作为paramtype

发布于 2025-02-04 13:53:42 字数 2260 浏览 4 评论 0原文

我想这样的API向我的API发送请求:

{
    "id": "22",
    "code": "AXB23, ADR40, SMRS2"
}

并且我想在“ select ... where ... in ... in(axb23,adr40,smrs2)”中使用每个代码,但我不知道我不知道如何。 我发现在这里“ name =”代码“ paramtype =“ array” sqltype =“ string”/> 但这无效。 有什么建议吗?

编辑*: 这是我的api(不序列 - 忘记了原始帖子中请求的ID,我只有“代码”)

<inSequence>
    <property expression="json-eval($..code)" name="code" scope="default" type="STRING"/>
    <payloadFactory media-type="xml">
        <format>
            <selectTEST xmlns:xs="http://ws.wso2.org/dataservice/selectTEST">
                <code>$1</code>
            </selectTEST>
        </format>
        <args>
            <arg evaluator="xml" expression="$ctx:code"/>
        </args>
    </payloadFactory>
            
    <property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
    <header name="Action" scope="default" value="selectTEST"/>
    <call>
        <endpoint key="ep.select"/>
    </call>
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            
    <respond/>
</inSequence>

,这是我的数据服务:

<operation name="selectTEST" returnRequestStatus="true">
        <call-query href="selectTEST">
            <with-param name="code" query-param="code"/>
        </call-query>
</operation>

<query id="selectTEST" useConfig="default">
        <sql><![CDATA[
        select x, y, z, w from mydatabase where code in (:code)
        ]]></sql>
            <param type="IN" name="code" paramType="ARRAY" sqlType="STRING"/>
        <result outputType="json">
        {
          "data": {
            "records": [
              {
                "x": "$x",
                "y": "$y",
                "z": "$z",
                "w": "$w"
              }
            ]
          }
        }
        </result>
</query>

我的请求:

{ 
    "code": ["AXB23", "ADR40"]
}

I want to sent a request to my api like this:

{
    "id": "22",
    "code": "AXB23, ADR40, SMRS2"
}

And I want to use every code in a query like "SELECT ... WHERE ... IN(AXB23, ADR40, SMRS2)" in my datasource but I don't know how.
I found here a solution with <param type="IN" name="code" paramType="ARRAY" sqlType="STRING"/>
but it doesn't work.
Any suggestions?

EDIT*:
This is my api (inSequence - forget about the id from request in the original post, I just only have that 'code')

<inSequence>
    <property expression="json-eval($..code)" name="code" scope="default" type="STRING"/>
    <payloadFactory media-type="xml">
        <format>
            <selectTEST xmlns:xs="http://ws.wso2.org/dataservice/selectTEST">
                <code>$1</code>
            </selectTEST>
        </format>
        <args>
            <arg evaluator="xml" expression="$ctx:code"/>
        </args>
    </payloadFactory>
            
    <property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
    <header name="Action" scope="default" value="selectTEST"/>
    <call>
        <endpoint key="ep.select"/>
    </call>
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            
    <respond/>
</inSequence>

and this is my dataservice:

<operation name="selectTEST" returnRequestStatus="true">
        <call-query href="selectTEST">
            <with-param name="code" query-param="code"/>
        </call-query>
</operation>

<query id="selectTEST" useConfig="default">
        <sql><![CDATA[
        select x, y, z, w from mydatabase where code in (:code)
        ]]></sql>
            <param type="IN" name="code" paramType="ARRAY" sqlType="STRING"/>
        <result outputType="json">
        {
          "data": {
            "records": [
              {
                "x": "$x",
                "y": "$y",
                "z": "$z",
                "w": "$w"
              }
            ]
          }
        }
        </result>
</query>

My request:

{ 
    "code": ["AXB23", "ADR40"]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-02-11 13:53:42

如果使用数组类型参数,则应将值作为正确的JSON数组传递。这样的事情对您有用。

请求:

{
  "_poststudent": {
    "code": ["AXB23","ADR40"]
  }
}

dataService查询

<query id="CreateStudents" useConfig="default">
    <sql>SELECT id, name, school, grade FROM students WHERE grade IN (:code)</sql>
    <param name="code" paramType="ARRAY" sqlType="STRING" type="IN" optional="false" />
    <result outputType="json">{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school",                        "grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school", "grade":"$grade"} ] }}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"} ] } }</result>
  </query>

更新

完整的数据服务配置。

<data name="RESTDataService" serviceNamespace="http://ws.wso2.org/dataservice/samples/json_sample" transports="http https">
  <description>Exposing the data service as a REST service.</description>
  <config id="default">
    <property name="driverClassName">com.mysql.jdbc.Driver</property>
    <property name="url">jdbc:mysql://localhost:3306/school_db</property>
    <property name="org.wso2.ws.dataservice.user">xxxxx</property>
    <property name="org.wso2.ws.dataservice.password">xxxxx</property>
  </config>
  <resource method="POST" path="student">
    <call-query href="ReadStudents">
      <with-param name="code" query-param="code" />
    </call-query>
  </resource>
  <operation name="selectTEST" returnRequestStatus="true">
    <call-query href="ReadStudents">
      <with-param name="code" query-param="code" />
    </call-query>
  </operation>
  <query id="ReadStudents" useConfig="default">
    <sql>SELECT id, name, school, grade FROM students WHERE grade IN (:code)</sql>
    <param name="code" paramType="ARRAY" sqlType="STRING" type="IN" optional="false" />
    <result outputType="json">{               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      </result>
  </query>
</data>

请注意,我已经指定了&lt;资源方法=“ path”路径=“ student”&gt;。资源用于RESTFULL INCOCACTICS,&lt;操作名称=“ selectTest” returnRequestStatus =“ true”&gt;用于肥皂调用。我已经创建了Rentful and SOAP服务供您参考。以下是您可以调用它们的方式。

注意:这里我直接调用数据服务。

RESTFULL调用。

curl --location --request POST 'http://localhost:8290/services/RESTDataService/student' \
--header 'Content-Type: application/json' \
--data-raw '{
  "students": {
    "code": ["AXB23", "ADR40"]
  }
}'

SOAP服务调用

curl --location --request POST 'http://localhost:8290/services/RESTDataService/selectTEST' \
--header 'Action: selectTEST' \
--header 'Content-Type: text/xml' \
--header 'Accept: application/json' \
--data-raw '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
    <soap:Body>
        <code>AXB23</code>
        <code>ADR40</code>
    </soap:Body>
</soap:Envelope>'

注意:在XML消息中,数组被描绘为重复元素,如上所示。

If you are using the array type parameters you should pass the values as a proper JSON array. Something like this will work for you.

The Request:

{
  "_poststudent": {
    "code": ["AXB23","ADR40"]
  }
}

Dataservice Query

<query id="CreateStudents" useConfig="default">
    <sql>SELECT id, name, school, grade FROM students WHERE grade IN (:code)</sql>
    <param name="code" paramType="ARRAY" sqlType="STRING" type="IN" optional="false" />
    <result outputType="json">{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school",                        "grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"}]}}{"students":{"student":[{"name":"$name","school":"$school", "grade":"$grade"} ] }}{"students":{"student":[{"name":"$name","school":"$school","grade":"$grade"} ] } }</result>
  </query>

Update

The Full Dataservice config.

<data name="RESTDataService" serviceNamespace="http://ws.wso2.org/dataservice/samples/json_sample" transports="http https">
  <description>Exposing the data service as a REST service.</description>
  <config id="default">
    <property name="driverClassName">com.mysql.jdbc.Driver</property>
    <property name="url">jdbc:mysql://localhost:3306/school_db</property>
    <property name="org.wso2.ws.dataservice.user">xxxxx</property>
    <property name="org.wso2.ws.dataservice.password">xxxxx</property>
  </config>
  <resource method="POST" path="student">
    <call-query href="ReadStudents">
      <with-param name="code" query-param="code" />
    </call-query>
  </resource>
  <operation name="selectTEST" returnRequestStatus="true">
    <call-query href="ReadStudents">
      <with-param name="code" query-param="code" />
    </call-query>
  </operation>
  <query id="ReadStudents" useConfig="default">
    <sql>SELECT id, name, school, grade FROM students WHERE grade IN (:code)</sql>
    <param name="code" paramType="ARRAY" sqlType="STRING" type="IN" optional="false" />
    <result outputType="json">{               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      {               "students":{                  "student":[                     {                        "name":"$name",                        "school":"$school",                        "grade":"$grade"                     }                  ]               }            }      </result>
  </query>
</data>

Note that I have specified a <resource method="POST" path="student">. resources are for RESTfull invocations, and <operation name="selectTEST" returnRequestStatus="true"> are for SOAP calls. I have created both RestFul and SOAP services for your reference. Following is how you can invoke them.

Note: Here I'm directly calling the Dataservice.

RestFull Invocation.

curl --location --request POST 'http://localhost:8290/services/RESTDataService/student' \
--header 'Content-Type: application/json' \
--data-raw '{
  "students": {
    "code": ["AXB23", "ADR40"]
  }
}'

SOAP Service Call

curl --location --request POST 'http://localhost:8290/services/RESTDataService/selectTEST' \
--header 'Action: selectTEST' \
--header 'Content-Type: text/xml' \
--header 'Accept: application/json' \
--data-raw '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
    <soap:Body>
        <code>AXB23</code>
        <code>ADR40</code>
    </soap:Body>
</soap:Envelope>'

Note: In a XML message an Array is depicted as repeating elements as shown above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文