You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 DataService API转JSON为SOAP时缺失信封与Body的解决方法

问题描述

用WSO2 DataService创建了返回JSON格式的API,但客户端要求接收SOAP响应。尝试通过datamapper转换格式后,仅得到不含SOAP信封与SOAP Body的XML响应。

期望的SOAP响应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetFacturesClientResponse xmlns="http://tempuri.org/">
<GetFacturesClientResult>
<code>0</code>
<codeClient>P-2008-043681</codeClient>
<nom>na</nom>
<prenom>ouss</prenom>
</GetFacturesClientResult>
</GetFacturesClientResponse>
</soap:Body>
</soap:Envelope>

实际得到的响应

<GetFacturesClientResponse xmlns="http://tempuri.org/">
<GetFacturesClientResult>
<code>0</code>
<codeClient>P-2008-043681</codeClient>
<nom>na</nom>
<prenom>ouss</prenom>
</GetFacturesClientResult>
</GetFacturesClientResponse>

当前datamapper配置

<datamapper config="gov:datamapper/oussama.dmc" inputSchema="gov:datamapper/oussama_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/oussama_outputSchema.json" outputType="JSON" xsltStyleSheet="gov:datamapper/oussama_xsltStyleSheet.xml"/>

解决方法

方法1:调整DataMapper配置并通过XSLT添加SOAP信封

  1. 修正datamapper输出类型:当前配置的outputType为JSON,需要改为XML,同时将输出Schema替换为对应的XML Schema:
    <datamapper config="gov:datamapper/oussama.dmc" inputSchema="gov:datamapper/oussama_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/oussama_outputSchema.xml" outputType="XML" xsltStyleSheet="gov:datamapper/oussama_xsltStyleSheet.xml"/>
    
  2. 修改XSLT样式表:在oussama_xsltStyleSheet.xml中添加SOAP信封包装逻辑,将转换后的XML内容嵌入SOAP Body:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/">
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <soap:Body>
            <xsl:copy-of select="."/>
          </soap:Body>
        </soap:Envelope>
      </xsl:template>
    </xsl:stylesheet>
    

方法2:使用WSO2内置的SOAP信封中介

如果不想修改XSLT,可以在API的处理序列中添加Envelope中介,直接为XML响应套上标准SOAP信封:

<envelope>
   <target format="soap11"/>
</envelope>

将该中介放在datamapper之后即可,若需要SOAP 1.2格式,将format参数改为soap12。

注意事项

  • 确保datamapper输出的XML结构与期望的GetFacturesClientResponse完全一致,且正确配置http://tempuri.org/命名空间。
  • 检查API的响应类型配置,不要强制限定为JSON,需允许XML/SOAP类型响应返回。

内容的提问来源于stack exchange,提问作者Oussama Nairi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:20:26