无法以 JSON 格式将参数传递给 REST API WSO2

发布于 2025-01-14 08:59:50 字数 3124 浏览 2 评论 0原文

有谁知道这个错误? 我将 json 传递给请求,例如:

{"param1":"hello world"}

Property Mediator 动作:设置, 将动作设置为:表达式, 类型:字符串, 表达式:json-eval($.param1), 范围:axis2

似乎API无法提取“hello world”值,因为日志显示:

param1=null

与值完美配合:

  <property name="param1" value="HELLO WORLD" scope="default" type="STRING"/>

Null这样:

 <property name="param1" expression="json-eval($.param1)" scope="axis2" type="STRING"/>

API源代码视图:

<api xmlns="http://ws.apache.org/ns/synapse" name="REST" context="/api/post">
   <resource methods="POST" url-mapping="/createInvoice">
      <inSequence>
         <property name="operation" value="createInvoice" scope="default" type="STRING"/>
         <log level="full" separator="|">
            <property name="operation" expression="get-property('operation')"/>
            <property name="step" value="request"/>
         </log>
         <property name="param1" expression="json-eval($.param1)" scope="axis2" type="STRING"/>
         <property name="param2" expression="json-eval($.param2)" scope="default" type="STRING"/>
         <log level="full" separator="|">
            <property name="param1" expression="get-property('Axis2','param1')"/>
            <property name="param2" expression="$ctx:param2"/>
         </log>
         <dbreport>
            <connection>
               <pool>
                  <dsName>OEBS_DS_MOS</dsName>
               </pool>
            </connection>
            <statement>
               <sql>call procedure.create_invoice(?,?)</sql>
               <parameter expression="get-property('Axis2','param1')" type="VARCHAR"/>
               <parameter expression="$ctx:param2" type="VARCHAR"/>
            </statement>
         </dbreport>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "00", "message" : "success"}}</format>
            <args/>
         </payloadFactory>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
         <property name="contentType" value="application/json" scope="axis2" type="STRING"/>
         <respond/>
      </inSequence>
      <outSequence>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "02", "message" : "outSeq"}}</format>
            <args/>
         </payloadFactory>
      </outSequence>
      <faultSequence>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "01", "message" : "fail"}}</format>
            <args/>
         </payloadFactory>
      </faultSequence>
   </resource>
</api>
                    

Does anyone know about this error?
I'm passing json to the request like:

{"param1":"hello world"}

Property Mediator
action:set,
set action as: expression,
type: string,
expression: json-eval($.param1),
scope: axis2

Seems like API cannot extract the "hello world" value, because logs show:

param1=null

Works perfectly with value:

  <property name="param1" value="HELLO WORLD" scope="default" type="STRING"/>

Null this way:

 <property name="param1" expression="json-eval($.param1)" scope="axis2" type="STRING"/>

API Source View:

<api xmlns="http://ws.apache.org/ns/synapse" name="REST" context="/api/post">
   <resource methods="POST" url-mapping="/createInvoice">
      <inSequence>
         <property name="operation" value="createInvoice" scope="default" type="STRING"/>
         <log level="full" separator="|">
            <property name="operation" expression="get-property('operation')"/>
            <property name="step" value="request"/>
         </log>
         <property name="param1" expression="json-eval($.param1)" scope="axis2" type="STRING"/>
         <property name="param2" expression="json-eval($.param2)" scope="default" type="STRING"/>
         <log level="full" separator="|">
            <property name="param1" expression="get-property('Axis2','param1')"/>
            <property name="param2" expression="$ctx:param2"/>
         </log>
         <dbreport>
            <connection>
               <pool>
                  <dsName>OEBS_DS_MOS</dsName>
               </pool>
            </connection>
            <statement>
               <sql>call procedure.create_invoice(?,?)</sql>
               <parameter expression="get-property('Axis2','param1')" type="VARCHAR"/>
               <parameter expression="$ctx:param2" type="VARCHAR"/>
            </statement>
         </dbreport>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "00", "message" : "success"}}</format>
            <args/>
         </payloadFactory>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
         <property name="contentType" value="application/json" scope="axis2" type="STRING"/>
         <respond/>
      </inSequence>
      <outSequence>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "02", "message" : "outSeq"}}</format>
            <args/>
         </payloadFactory>
      </outSequence>
      <faultSequence>
         <payloadFactory media-type="json">
            <format>{"response" : {"code" : "01", "message" : "fail"}}</format>
            <args/>
         </payloadFactory>
      </faultSequence>
   </resource>
</api>
                    

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

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

发布评论

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

评论(1

感情旳空白 2025-01-21 08:59:50

问题出在您的财产的范围上。
您将 JSON 中的值设置为 scope="axis2" 中的属性,因此您可以使用以下方法读取该属性:get-property('axis2', 'param1'),或者$axis2:param1

或者,您可以在默认范围内设置属性,现在您可以使用:get-property('param1')$ctx:param1

这个文档准确地解释了这一点。

The problem is with scope of your property.
You set value from JSON to property in scope="axis2", so you can read that property using: get-property('axis2', 'param1'), or $axis2:param1.

Or you can set property in default scope, and now you can use: get-property('param1'), or $ctx:param1.

This documentation explains it exactly.

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