WSO2 DataMapper 将 XML 转换为 SOAP 时出错

Posted

技术标签:

【中文标题】WSO2 DataMapper 将 XML 转换为 SOAP 时出错【英文标题】:WSO2 DataMapper Error converting XML to SOAP 【发布时间】:2020-02-27 20:02:08 【问题描述】:

我必须将我的后端肥皂响应转换为 json。为此,我使用数据映射器 XML 到 JSON。数据映射器在运行时失败并出现以下错误:

[2019-11-01 14:58:29,805] [micro-integrator] ERROR - DataMapperMediator DataMapper mediator : mapping failed
Error while parsing XML input stream. Current context not Object but root
    at org.wso2.carbon.mediator.datamapper.engine.input.readers.XMLInputReader.read(XMLInputReader.java:124)
    at org.wso2.carbon.mediator.datamapper.engine.input.InputBuilder.buildInputModel(InputBuilder.java:59)
    at org.wso2.carbon.mediator.datamapper.engine.core.mapper.MappingHandler.doMap(MappingHandler.java:90)
    at org.wso2.carbon.mediator.datamapper.DataMapperMediator.transform(DataMapperMediator.java:390)
    at org.wso2.carbon.mediator.datamapper.DataMapperMediator.mediate(DataMapperMediator.java:301)
    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:370)
    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:58)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:325)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:578)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:195)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:265)
    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)
[2019-11-01 14:58:29,807] [micro-integrator] ERROR - AgendaRestApi DataMapper mediator : mapping failed
Error while parsing XML input stream. Current context not Object but root
    at org.wso2.carbon.mediator.datamapper.engine.input.readers.XMLInputReader.read(XMLInputReader.java:124)
    at org.wso2.carbon.mediator.datamapper.engine.input.InputBuilder.buildInputModel(InputBuilder.java:59)
    at org.wso2.carbon.mediator.datamapper.engine.core.mapper.MappingHandler.doMap(MappingHandler.java:90)
    at org.wso2.carbon.mediator.datamapper.DataMapperMediator.transform(DataMapperMediator.java:390)
    at org.wso2.carbon.mediator.datamapper.DataMapperMediator.mediate(DataMapperMediator.java:301)
    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:370)
    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:58)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:325)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:578)
    at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:195)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:265)
    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)

用于配置数据映射器的输入和输出示例文件是: 输入

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetSpecialtiesResponse xmlns="http://agenda/">
         <GetSpecialtiesResult xmlns:a="http://schemas.datacontract.org/2004/07/WsAgenda.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Specialty>
               <a:code>cardiology</a:code>
               <a:name>Cardiologie</a:name>
            </a:Specialty>
            <a:Specialty>
               <a:code>neurology</a:code>
               <a:name>Neurologie</a:name>
            </a:Specialty>
         </GetSpecialtiesResult>
      </GetSpecialtiesResponse>
   </s:Body>
</s:Envelope>

输出:

"Specialties": [
      
      "id": "cardiology",
      "description": "Cardiologie"
   ,
     
      "id": "cardiology",
      "description": "Cardiologie"
   
]

在数据映射器设计器中似乎一切正常。 下面是序列码:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/agenda" name="AgendaRestApi" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET" url-mapping="/specialties">
        <inSequence>
            <log/>
            <payloadFactory description="" media-type="xml">
                <format>
                    <soapenv:Envelope xmlns:agen="http://agenda/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <agen:GetSpecialties/>
                        </soapenv:Body>
                    </soapenv:Envelope>
                </format>
                <args/>
            </payloadFactory>
            <header name="soapAction" scope="transport" value="http://agenda/agenda/GetSpecialties"/>
            <send>
                <endpoint key="AgendaEP"/>
            </send>
        </inSequence>
        <outSequence>
            <datamapper config="gov:datamapper/GetSpecialtiesMapping.dmc" inputSchema="gov:datamapper/GetSpecialtiesMapping_inputSchema.json" inputType="XML" outputSchema="gov:datamapper/GetSpecialtiesMapping_outputSchema.json" outputType="JSON" xsltStyleSheet="gov:datamapper/GetSpecialtiesMapping_xsltStyleSheet.xml"/>
            <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            <respond/>
        </outSequence>
        <faultSequence>
            <log level="full"/>
        </faultSequence>
    </resource>
</api>

欢迎所有可能的解决方案:)

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,解决了在 datamapper 中介之前添加这个属性。

<property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
<datamapper config="gov:datamapper/GetSpecialtiesMapping.dmc" inputSchema="gov:datamapper/GetSpecialtiesMapping_inputSchema.json" inputType="XML" outputSchema="gov:datamapper/GetSpecialtiesMapping_outputSchema.json" outputType="JSON" xsltStyleSheet="gov:datamapper/GetSpecialtiesMapping_xsltStyleSheet.xml"/>

希望这会有所帮助, JP

【讨论】:

以上是关于WSO2 DataMapper 将 XML 转换为 SOAP 时出错的主要内容,如果未能解决你的问题,请参考以下文章

WSO2 Enterprise Integrator 6.6.0 使用 XSLT 将 XML 响应转换为 Json

WSO2 ESB 无法将完整的 JSON 数据转换为 XML

WSO2 API Manager将JSON转换为XML(并且失败了请求)

wso2转换,xml到json转换

WSO2 AM 1.9 - 将CDATA保留在有效负载工厂中

如何在 WSO2 中以 XML 格式转换 JSON 消息