如何在 WSO2 中将 JSON 作为字符串发送?
我正尝试从 wso2 向 rabbitMQ 发送有效负载,它必须是一个字符串,但是当我尝试设置 payloadJSON 时,y 收到此错误
missing } after property list
但它是一个字符串,我不明白为什么,有人知道如何将此 JSON 转换为字符串吗?
这是我的脚本
function main(mc) {
var log = mc.getServiceLog()
var enviroment = "dev";
var jsonPayload = JSON.parse(mc.getProperty('orderToERP'))
var orderToERP = "'"+ JSON.stringify(jsonPayload) + "'";
log.info(orderToERP)//'{"message":"message from terminal"}'
var rabbitBody = {
properties: {},
routing_key: "queue." + enviroment,
exchange: "queue." + enviroment,
payload:orderToERP,
payload_encoding: "string"
}
mc.setPayloadJSON(rabbitBody);
>
这是我收到的错误
[2021-04-28 10:55:04,378] INFO {CommonScriptMessageContext} - '{"message":"message from
terminal"}'
[2021-04-28 10:55:04,381] ERROR {ScriptMediator} - {api:OrderConfirm} The script engine returned an error executing the external js script : Value {name ='null', keyValue ='conf:scripts/rabittMPAdapter.js'} function main com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: `missing } after property list` (<Unknown source>#1) in <Unknown source> at line number 1 (<Unknown source>#15) in <Unknown source> at line number 15
at com.sun.phobos.script.javascript.RhinoScriptEngine.invokeMethod(RhinoScriptEngine.java:259)
at com.sun.phobos.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:216)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateWithExternalScript(ScriptMediator.java:344)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:288)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:258)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:171)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.rest.Resource.process(Resource.java:331)
at org.apache.synapse.rest.API.process(API.java:448)
at org.apache.synapse.rest.RESTRequestHandler.apiProcess(RESTRequestHandler.java:120)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:98)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:71)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:331)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:99)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:367)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:426)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:181)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.mozilla.javascript.WrappedException: Wrapped com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: missing } after property list (<Unknown source>#1) in <Unknown source> at line number 1 (<Unknown source>#15)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
at org.mozilla.javascript.gen._Unknown_source__4574._c_main_1(<Unknown source>:15)
at org.mozilla.javascript.gen._Unknown_source__4574.call(<Unknown source>)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen._Unknown_source__4574.call(<Unknown source>)
at com.sun.phobos.script.javascript.RhinoScriptEngine.invokeMethod(RhinoScriptEngine.java:245)
... 25 more
Caused by: com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: missing } after property list (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.phobos.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:184)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at org.apache.synapse.mediators.bsf.CommonScriptMessageContext.setPayloadJSON(CommonScriptMessageContext.java:970)
at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
... 33 more
Caused by: org.mozilla.javascript.EvaluatorException: missing } after property list (<Unknown source>#1)
at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)
at org.mozilla.javascript.DefaultErrorReporter.error(DefaultErrorReporter.java:64)
at org.mozilla.javascript.Parser.addError(Parser.java:188)
at org.mozilla.javascript.Parser.addError(Parser.java:166)
at org.mozilla.javascript.Parser.reportError(Parser.java:223)
at org.mozilla.javascript.Parser.reportError(Parser.java:217)
at org.mozilla.javascript.Parser.mustMatchToken(Parser.java:388)
at org.mozilla.javascript.Parser.mustMatchToken(Parser.java:378)
at org.mozilla.javascript.Parser.objectLiteral(Parser.java:3246)
at org.mozilla.javascript.Parser.primaryExpr(Parser.java:2767)
at org.mozilla.javascript.Parser.memberExpr(Parser.java:2406)
at org.mozilla.javascript.Parser.unaryExpr(Parser.java:2304)
at org.mozilla.javascript.Parser.mulExpr(Parser.java:2229)
at org.mozilla.javascript.Parser.addExpr(Parser.java:2213)
at org.mozilla.javascript.Parser.shiftExpr(Parser.java:2194)
at org.mozilla.javascript.Parser.relExpr(Parser.java:2169)
at org.mozilla.javascript.Parser.eqExpr(Parser.java:2141)
at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:2130)
at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:2119)
at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:2108)
at org.mozilla.javascript.Parser.andExpr(Parser.java:2097)
at org.mozilla.javascript.Parser.orExpr(Parser.java:2086)
at org.mozilla.javascript.Parser.condExpr(Parser.java:2062)
at org.mozilla.javascript.Parser.assignExpr(Parser.java:2033)
at org.mozilla.javascript.Parser.expr(Parser.java:2012)
at org.mozilla.javascript.Parser.parenExpr(Parser.java:2839)
at org.mozilla.javascript.Parser.primaryExpr(Parser.java:2773)
at org.mozilla.javascript.Parser.memberExpr(Parser.java:2406)
at org.mozilla.javascript.Parser.unaryExpr(Parser.java:2304)
at org.mozilla.javascript.Parser.mulExpr(Parser.java:2229)
at org.mozilla.javascript.Parser.addExpr(Parser.java:2213)
at org.mozilla.javascript.Parser.shiftExpr(Parser.java:2194)
at org.mozilla.javascript.Parser.relExpr(Parser.java:2169)
at org.mozilla.javascript.Parser.eqExpr(Parser.java:2141)
at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:2130)
at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:2119)
at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:2108)
at org.mozilla.javascript.Parser.andExpr(Parser.java:2097)
at org.mozilla.javascript.Parser.orExpr(Parser.java:2086)
at org.mozilla.javascript.Parser.condExpr(Parser.java:2062)
at org.mozilla.javascript.Parser.assignExpr(Parser.java:2033)
at org.mozilla.javascript.Parser.expr(Parser.java:2012)
at org.mozilla.javascript.Parser.statementHelper(Parser.java:1039)
at org.mozilla.javascript.Parser.statement(Parser.java:901)
at org.mozilla.javascript.Parser.parse(Parser.java:540)
at org.mozilla.javascript.Parser.parse(Parser.java:502)
at org.mozilla.javascript.Context.compileImpl(Context.java:2350)
at org.mozilla.javascript.Context.compileReader(Context.java:1296)
at org.mozilla.javascript.Context.compileReader(Context.java:1268)
at org.mozilla.javascript.Context.evaluateReader(Context.java:1107)
at com.sun.phobos.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:172)
... 39 more
提前致谢
你说
var orderToERP = "'"+ JSON.stringify(jsonPayload) + "'";
但是
JSON.stringify()
已经提供了一个完美的字符串。所以这是你的根本原因。你在解析器不期望的地方包装了一个字符串。
这样做
var orderToERP = JSON.stringify(jsonPayload);
要将 WSO2 中的消息发送到 RabbitMQ,您应该使用:
<send>
<endpoint>
<address uri="rabbitmq:/AMQPProducerSample?rabbitmq.server.host.name=localhost&rabbitmq.server.port=5672&rabbitmq.queue.name=queue&rabbitmq.queue.route.key=route&rabbitmq.exchange.name=exchange"/>
</endpoint>
</send>
请参阅文档: RabbitMQ 用例
我不知道为什么需要将 JSON 有效负载作为字符串发送,但像下面这样的 payloadFactory 对我来说是有效的。确保在 axis2.xml 中进行了配置:
<messageFormatter contentType="text/plain" class="org.apache.axis2.format.PlainTextFormatter"/>
。
<payloadFactory media-type="text">
<format>'$1'</format>
<args>
<arg evaluator="json" expression="json-eval($)" literal="false"/>
</args>
</payloadFactory>
通过 API 发送 JSON
{"message":"message from terminal">
,经过调解后,我进入了 RabbitMQ:
如果这不起作用。提供有关 wso2 版本的更多信息。
需要澄清的一点:
var rabbitBody = {
properties: {},
routing_key: "queue." + enviroment,
exchange: "queue." + enviroment,
payload:orderToERP,
payload_encoding: "string"
}
这是一个 JavaScript 对象。它是您 JavaScript 代码中的数据。它看起来很像 JSON,但它不是 JSON。JSON 是一种用于传输数据的字符串格式。要将 JavaScript 对象转换为 JSON 字符串,请使用
JSON.stringify()
:
mc.setPayloadJSON(JSON.stringify(rabbitBody));
我认为了解 JSON 字符串和 JavaScript 对象之间的区别很重要。这将帮助您了解在不同情况下该怎么做。