如何解决将 javaScript 添加到 wso2 中的错误?
how resolve error in Adding javaScript to wso2?
我正在尝试添加此脚本(用于分组 json 响应),它在在线 jsvascript 测试中有效,但在 wso2 中它 returns 202 Accepted in postman 和 return 空("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.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
谁能帮帮我?我花了2个星期解决了这个问题..
因为Wso2不接受javascript中的任何方法,所以你应该写javascript没有任何方法的基本格式的代码。
我正在尝试添加此脚本(用于分组 json 响应),它在在线 jsvascript 测试中有效,但在 wso2 中它 returns 202 Accepted in postman 和 return 空("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.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
谁能帮帮我?我花了2个星期解决了这个问题..
因为Wso2不接受javascript中的任何方法,所以你应该写javascript没有任何方法的基本格式的代码。