I'm trying to add this script (for grouping json response) , it is works in online jsvascript test but in wso2 it returns 202 Accepted in postman and return empty (the "field" is the name of json array and "Libraryid" the key I want to grouped by :
<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>
the error is :
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
can anyone help me ? I spent 2 weeks to solve the problem ..
Because Wso2 does not accept any methods in javascript , so you should write javascript code in basic format without any methods .