如何在wso2 esb中传递json体中的&符号

时间:2016-08-10 03:28:54

标签: json rest wso2 wso2esb ampersand

WSO2 esb上调用REST API时,在JSON正文值中传递&符号时出现问题,它会出现异常。但我需要通过这个'&'通过esb而不是&的后端值。

我尝试通过以下方式发送,但没有取得任何成功。

  • &
  • url编码值
  • 十六进制值

请帮我解决这个问题。

这是我得到的例外。

[2016-08-10 13:52:26,538] ERROR -  The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): ' ' (<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:291)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:235)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:203)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
        at org.apache.synapse.rest.Resource.process(Resource.java:297)
        at org.apache.synapse.rest.API.process(API.java:341)
        at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:76)
        at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:63)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220)
        at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344)
        at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:385)
        at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:183)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): ' ' (<Unknown Source>#1)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
        at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
        at org.mozilla.javascript.xml.impl.xmlbeans.XML.createFromJS(XML.java:388)
        at org.mozilla.javascript.xml.impl.xmlbeans.XML.jsConstructor(XML.java:3065)
        at org.mozilla.javascript.xml.impl.xmlbeans.XMLObjectImpl.execIdCall(XMLObjectImpl.java:549)
        at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127)
        at org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:313)
        at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2132)
        at org.mozilla.javascript.gen.c92._c0(<Unknown Source>:1)
        at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
        at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
        at org.mozilla.javascript.gen.c92.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 21 more
[2016-08-10 13:52:26,540]  WARN -  Executing fault handler due to exception encountered {org.apache.synapse.core.axis2.SynapseMessageReceiver}
[2016-08-10 13:52:26,540]  WARN -  ERROR_CODE : 0 {org.apache.synapse.FaultHandler}
[2016-08-10 13:52:26,541]  WARN -  ERROR_MESSAGE : The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.FaultHandler}
[2016-08-10 13:52:26,541]  WARN -  ERROR_DETAIL : org.apache.synapse.SynapseException: The script engine returned an error executing the inlined js script function mediate
        at org.apache.synapse.mediators.AbstractMediator.handleException(AbstractMediator.java:313)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:241)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:203)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
        at org.apache.synapse.rest.Resource.process(Resource.java:297)
        at org.apache.synapse.rest.API.process(API.java:341)
        at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:76)
        at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:63)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220)
        at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344)
        at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:385)
        at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:183)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): ' ' (<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:291)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:235)
        ... 18 more
Caused by: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): ' ' (<Unknown Source>#1)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
        at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
        at org.mozilla.javascript.xml.impl.xmlbeans.XML.createFromJS(XML.java:388)
        at org.mozilla.javascript.xml.impl.xmlbeans.XML.jsConstructor(XML.java:3065)
        at org.mozilla.javascript.xml.impl.xmlbeans.XMLObjectImpl.execIdCall(XMLObjectImpl.java:549)
        at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127)
        at org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:313)
        at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2132)
        at org.mozilla.javascript.gen.c92._c0(<Unknown Source>:1)
        at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
        at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
        at org.mozilla.javascript.gen.c92.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 21 more

2 个答案:

答案 0 :(得分:1)

我已尝试启用有线日志,但它对我没有任何改动。您尝试过的ESB版本是什么?如果您启用了线路日志,那么您将会发生什么以及如下所示的内容。我添加了&amp;符号到身体参数。

[2016-08-10 11:11:05,643] DEBUG - wire >> "POST /pizzashack/1.0.0/order HTTP/1.1[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Host: 172.17.0.1:8243[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept: application/json[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept-Language: en-US,en;q=0.5[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept-Encoding: gzip, deflate, br[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Content-Type: application/json[\r][\n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Authorization: Bearer 0227aad0-bc8c-3a4a-b010-fadb7582b488[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Referer: https://127.0.0.1:9443/store/apis/info?name=PizzaShackAPI&version=1.0.0&provider=admin[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Content-Length: 169[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "origin: https://127.0.0.1:9443[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Connection: keep-alive[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "[\r][\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "{[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "customerName": "string&test",[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "delivered": true,[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "address": "string",[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "pizzaType": "string",[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "creditCardNumber": "string",[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "quantity": 0,[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "  "orderId": 0[\n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "}"
[2016-08-10 11:11:05,647] DEBUG - wire << "POST /am/sample/pizzashack/v1/api/order HTTP/1.1[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept: application/json[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "origin: https://127.0.0.1:9443[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Referer: https://127.0.0.1:9443/store/apis/info?name=PizzaShackAPI&version=1.0.0&provider=admin[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept-Encoding: gzip, deflate, br[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept-Language: en-US,en;q=0.5[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Content-Type: application/json; charset=UTF-8[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Host: localhost:9443[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "a9[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "{[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "customerName": "string&test",[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "delivered": true,[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "address": "string",[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "pizzaType": "string",[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "creditCardNumber": "string",[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "quantity": 0,[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "  "orderId": 0[\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "}[\r][\n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "0[\r][\n]"
[2016-08-10 11:11:05,648] DEBUG - wire << "[\r][\n]"

答案 1 :(得分:1)

我也在ESB 4.8.1中尝试过相同的操作。因为我在错误日志中看到了java脚本错误,所以我也添加了java脚本调解器。请参阅下面的示例API。

   <api name="TestAPI" context="/test">
      <resource methods="POST" url-mapping="/status" faultSequence="fault">
         <inSequence>
            <script language="js">var symbol = mc.getPayloadXML()..*::password.toString();
               mc.setPayloadXML(
                  &lt;m:getQuote xmlns:m="http://services.samples/xsd"&gt;
                     &lt;m:request&gt;
                        &lt;m:symbol&gt;{symbol}&lt;/m:symbol&gt;
                     &lt;/m:request&gt;
                  &lt;/m:getQuote&gt;);</script>
            <send>
               <endpoint name="test-I_APIproductionEndpoint_0">
                  <http uri-template="http://127.0.0.1:8888/"/>
               </endpoint>
            </send>
         </inSequence>
         <outSequence>
            <send/>
         </outSequence>
      </resource>
   </api>

然后我发送请求如下。

curl -v -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz & abc"}' http://127.0.0.1:8280/test/status

我可以看到我的值正确传递到后端。只是为了验证我在ESB和后端之间添加了TCPMon来验证输出消息。它回归&amp;标志。有线日志在这里,TCPMon输出也附加。

[2016-08-10 14:30:21,236] DEBUG - wire >> "POST /test/status HTTP/1.1[\r][\n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Host: 127.0.0.1:8280[\r][\n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "User-Agent: curl/7.43.0[\r][\n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Accept: */*[\r][\n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Content-Type: application/json[\r][\n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Content-Length: 41[\r][\n]"
[2016-08-10 14:30:21,237] DEBUG - wire >> "[\r][\n]"
[2016-08-10 14:30:21,237] DEBUG - wire >> "{"username":"xyz","password":"xyz & abc"}"
[2016-08-10 14:30:21,245] DEBUG - wire << "POST /status HTTP/1.1[\r][\n]"
[2016-08-10 14:30:21,245] DEBUG - wire << "Content-Type: application/json[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Accept: */*[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Host: 127.0.0.1:8888[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "[\r][\n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "2f[\r][\n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "{"getQuote":{"request":{"symbol":"xyz & abc"}}}[\r][\n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "0[\r][\n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "[\r][\n]"

enter image description here