使用 Script Mediator 在 WSO Api manager 3.2 中将 XML 转换为 JSON

Transforming XML to JSON in WSO Api manager 3.2 using Script Mediator

描述: 我正在尝试使用 wso2 3.2 api 管理器中的脚本中介从 xml 转换为 json。我面临的问题是脚本调解器方法 getPayloadXML() 无法从 xml 中提取特定值,它总是 returns 因为 ReferenceError: "code" is not defined。我尝试了多种方法,但无法弄清楚如何实现这一目标。如果有人提供帮助,我们将不胜感激。

响应调解

<sequence xmlns="http://ws.apache.org/ns/synapse" name="transform">    
<script language="js"><![CDATA[
   var envelope = mc.getPayloadXML()..*::fault-code.toString()
    
   var request ={"data":{"success":"true","message":"SuccessFull","faultcode":envelope}};

    mc.setPayloadJSON(request);
]]></script>
   <log level="full">
        <property name="Message" value="UpdatedMessage"/>
    </log>
    <property name="HTTP_SC" value="200" scope="axis2" />
   <property name="messageType" value="application/json" scope="axis2"/>
</sequence>

原始回复

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <wsa:MessageID>210419-BA7CF-32444</wsa:MessageID>
        <wsa:RelatesTo>Test123456</wsa:RelatesTo>
        <wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
        <wsa:From>
            <wsa:Address>some data</wsa:Address>
        </wsa:From>
        <wsa:Action>some data</wsa:Action>
    </soapenv:Header>
    <soapenv:Body>
        <cbo:response xmlns:abr="url" xmlns:asic="url" client-reference1="A1" response-type="error" version="1.2">
            <cbo:error>
                <cbo:fault-code>CBOX017</cbo:fault-code>
                <cbo:fault-string>User account is inactive.</cbo:fault-string>
                <cbo:fault-actor/>
                <cbo:detail/>
            </cbo:error>
        </cbo:response>
    </soapenv:Body>
</soapenv:Envelope>

预期响应(转换为 JSON)

{
    "data": {
        "success": "true",
        "message": "SuccessFull",
        "faultcode":CBOX017
    }
}

错误日志

TID: [-1234] [] [2021-04-19 16:14:41,259] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} - {api:admin--equifaxACNCheck:v1.0.0} The script engine returned an error executing the inlined js script function mediate com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: ReferenceError: "code" is not defined. (<Unknown Source>#2) in <Unknown Source> at line number 2
    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.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.mediate_aroundBody0(APIManagerExtensionHandler.java:73)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.mediate(APIManagerExtensionHandler.java:58)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.handleResponse_aroundBody4(APIManagerExtensionHandler.java:132)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.handleResponse(APIManagerExtensionHandler.java:120)
    at org.apache.synapse.rest.API.process(API.java:371)
    at org.apache.synapse.rest.RESTRequestHandler.apiProcessNonDefaultStrategy(RESTRequestHandler.java:144)
    at org.apache.synapse.rest.RESTRequestHandler.identifyAPI(RESTRequestHandler.java:164)
    at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:90)
    at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:60)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:331)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
    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.EcmaError: ReferenceError: "code" is not defined. (<Unknown Source>#2)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
    at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
    at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1728)
    at org.mozilla.javascript.gen._Unknown_Source__2233._c_script_0(<Unknown Source>:2)
    at org.mozilla.javascript.gen._Unknown_Source__2233.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__2233.call(<Unknown Source>)
    at org.mozilla.javascript.gen._Unknown_Source__2233.exec(<Unknown Source>)
    at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
    ... 25 more

TID: [-1234] [] [2021-04-19 16:14:41,267] ERROR {API_LOGGER.admin--equifaxACNCheck} - The script engine returned an error executing the inlined js script function mediate com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: ReferenceError: "code" is not defined. (<Unknown Source>#2) in <Unknown Source> at line number 2
    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.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.mediate_aroundBody0(APIManagerExtensionHandler.java:73)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.mediate(APIManagerExtensionHandler.java:58)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.handleResponse_aroundBody4(APIManagerExtensionHandler.java:132)
    at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.handleResponse(APIManagerExtensionHandler.java:120)
    at org.apache.synapse.rest.API.process(API.java:371)
    at org.apache.synapse.rest.RESTRequestHandler.apiProcessNonDefaultStrategy(RESTRequestHandler.java:144)
    at org.apache.synapse.rest.RESTRequestHandler.identifyAPI(RESTRequestHandler.java:164)
    at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:90)
    at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:60)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:331)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
    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.EcmaError: ReferenceError: "code" is not defined. (<Unknown Source>#2)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
    at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
    at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1728)
    at org.mozilla.javascript.gen._Unknown_Source__2233._c_script_0(<Unknown Source>:2)
    at org.mozilla.javascript.gen._Unknown_Source__2233.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__2233.call(<Unknown Source>)
    at org.mozilla.javascript.gen._Unknown_Source__2233.exec(<Unknown Source>)
    at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
    ... 25 more

ScriptMediator 似乎在解析 fault-code 时出现问题。我认为这是因为连字符 - 在 JavaScript 和 E4X 中已经具有意义。也许您可以使用 PayloadFactory 的替代调解,如下所示:

    <sequence xmlns="http://ws.apache.org/ns/synapse" name="transform">    
       <payloadFactory media-type="json">
            <format>{"data":{"success":"", "message":"", "faultcode":""}}</format>
            <args>
                <arg literal="false" value="true"/>
                <arg literal="false" value="SuccessFull"/>
                <arg evaluator="xml" expression="//*:fault-code"
                    literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
            </args>
        </payloadFactory>
        <log level="full">
           <property name="Message" value="UpdatedMessage"/>
        </log>
        <property name="HTTP_SC" value="200" scope="axis2" />
    </sequence>

不需要 <property name="messageType" value="application/json" scope="axis2"/>,因为负载工厂应该设置它。