getPayloadJSON 返回一个空对象 [英] getPayloadJSON returning an empty object
问题描述
我正在做一些非常简单的脚本中介作为测试,看看我是否可以更改我的服务器返回的有效负载.
I am doing some very simple script mediation as a test to see if I can change the payload being returned by my server.
我的无中介payload如下:
My unmediated payload comes out as:
{"origin":"202.37.75.130"}
然后我尝试做一个非常简单的检查,看看有效载荷的 get 和 set 是否工作:
Then I try doing a very simple check to see whether get and set work for payloads:
<script language="js">
var older = mc.getPayloadJSON();
var newer=older;
mc.setPayloadJSON(newer);
</script>
我的结果是:
{"jsonObject":""}
我已经完成了测试,表明 setPayloadJSON() 有效,这意味着我的 getPayloadJSON 必须返回一个空对象.
I have done testing that shows that setPayloadJSON() works, which means that my getPayloadJSON must be returning an empty object.
查看日志文件显示:
ERROR {org.apache.synapse.mediators.bsf.ScriptMessageContext} - JSON object is null. {org.apache.synapse.mediators.bsf.ScriptMessageContext}
还有其他人遇到这个错误吗?
Is there anyone else having this error?
推荐答案
以防万一有人遇到同样的问题,我设法让我的工作正常工作.
Just in case someone has the same issue, I managed to get mine working.
我不得不采取一种解决方法:
I had to do a workaround method:
- 在脚本中介之前使用属性中介来设置属性:
属性名称="JSONPayload"表达式="json-eval($.)"
property name="JSONPayload" expression="json-eval($.)"
- 在脚本中介中,通过执行以下操作获取有效负载(作为字符串):
var pl_string = mc.getProperty("JSONPayload");
var pl_string = mc.getProperty("JSONPayload");
- 现在必须将字符串转换为 JS 对象:
var pl = JSON.parse(pl_string);
var pl = JSON.parse(pl_string);
- 像使用
getPayloadJSON()
获取负载一样操作负载.
- Manipulate the payload as if you'd obtained it using
getPayloadJSON()
.
我知道这是一种混搭的方法,但其他方法都不适合我,所以我尝试了这个方法.
I know it's a hacked together method, but none of the other ways were working for me so I tried this one.
一些信息,以防有人想调试它:
Some information in case anyone wants to debug it:
我使用的是 API 管理器 1.7.
I'm using API Manager 1.7.
我将消息构建器和格式化器设置为:生成器类 = "org.apache.synapse.commons.json.JsonStreamBuilder"格式化程序类 = "org.apache.synapse.commons.json.JsonStreamFormatter"
I set the message builders and formatters to these: Builder class = "org.apache.synapse.commons.json.JsonStreamBuilder" Formatter class = "org.apache.synapse.commons.json.JsonStreamFormatter"
这篇关于getPayloadJSON 返回一个空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!