我试图添加这个脚本(用于对json响应进行分组),它在在线jsvascript测试中有效,但在wso2中它返回202 Accepted in postman并返回空(“field”是json数组的名称,“Libraryid”是我想要分组的关键字:
<api xmlns="http://ws.apache.org/ns/synapse" name="obj" context="/obj">
<resource methods="POST GET" url-mapping="/get">
<inSequence>
<log/>
<script language="js">payload = mc.getPayloadJSON(); var result= payload.field.reduce(function(result, current) { result[current.Libraryid] = result[current.Libraryid] || []; result[current.Libraryid].push(current); return result;}, {}); mc.setPayloadJSON(result);</script>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<respond/>
</inSequence>
</resource>
</api>
字符串
错误是:
ERROR - obj The script engine returned an error executing the inlined js script function mediate
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String (<Unknown Source>#1) in <Unknown Source> at line number 1
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:395)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:290)
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.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.rest.Resource.process(Resource.java:363)
at org.apache.synapse.rest.API.process(API.java:431)
at org.apache.synapse.rest.RESTRequestHandler.apiProcess(RESTRequestHandler.java:135)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:113)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:71)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:325)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:92)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:338)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:383)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:152)
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 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String (<Unknown Source>#1)
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__480._c_script_0(<Unknown Source>:1)
at org.mozilla.javascript.gen._Unknown_Source__480.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__480.call(<Unknown Source>)
at org.mozilla.javascript.gen._Unknown_Source__480.exec(<Unknown Source>)
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
... 22 more
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.apache.synapse.mediators.bsf.CommonScriptMessageContext.serializeJson(CommonScriptMessageContext.java:996)
at org.apache.synapse.mediators.bsf.CommonScriptMessageContext.setPayloadJSON(CommonScriptMessageContext.java:957)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
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)
... 31 more
型
有人能帮我吗?我花了两个星期才解决这个问题。
1条答案
按热度按时间5jdjgkvh1#
因为Wso2不接受JavaScript中的任何方法,所以你应该以基本格式编写JavaScript代码,而不使用任何方法。