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

如何用独立版WireMock和JSON验证SOAP请求并返回对应响应

嘿,这个需求我刚好做过,用独立版WireMock完全可以通过JSON配置搞定,核心就是用它的XPath匹配器来校验SOAP请求的Header和Body元素,下面给你一步步拆解:

用独立版WireMock实现SOAP请求的Header&Body元素校验

核心逻辑

SOAP请求是XML格式,WireMock支持用XPath表达式精准定位XML里的元素,我们可以分别给Header和Body写匹配规则,满足条件就返回response1,不满足就返回response2。

首先处理Header的校验

针对你示例里的<a:Id>1876</a:Id>,得先注意SOAP请求里的命名空间(xmlns:a="http://abc.example.com/a"),XPath必须带上正确的命名空间才能匹配到元素。对应的匹配规则是这样的:

"headers": {
  "Content-Type": {
    "contains": "text/xml"
  },
  "soapenv:Header": {
    "matchesXPath": {
      "expression": "/soapenv:Envelope/soapenv:Header/a:abcHeaders/a:Id[text()='1876']",
      "namespaces": {
        "soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
        "a": "http://abc.example.com/a"
      }
    }
  }
}

这里先确保请求是SOAP类型(Content-Type包含text/xml),再用XPath精准匹配Header里的Id值。

然后是Body的校验(以示例里的<c:pqr>12</c:pqr>为例)

同样用XPath匹配Body里的特定元素,注意对应命名空间:

"bodyPatterns": [
  {
    "matchesXPath": {
      "expression": "/soapenv:Envelope/soapenv:Body/b:abcOpern/b:xyz/c:pqr[text()='12']",
      "namespaces": {
        "soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
        "b": "http://abc.example.com/b",
        "c": "http://abc.example.com/c"
      }
    }
  }
]

完整的Stub配置JSON

我们需要写两个Stub:第一个匹配所有满足Header和Body条件的请求,返回response1;第二个作为兜底,返回response2。

匹配成功返回response1的Stub

{
  "request": {
    "method": "POST",
    "url": "/your-soap-endpoint", // 替换成你的实际SOAP接口路径
    "headers": {
      "Content-Type": {
        "contains": "text/xml"
      }
    },
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "/soapenv:Envelope/soapenv:Header/a:abcHeaders/a:Id[text()='1876']",
          "namespaces": {
            "soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
            "a": "http://abc.example.com/a"
          }
        }
      },
      {
        "matchesXPath": {
          "expression": "/soapenv:Envelope/soapenv:Body/b:abcOpern/b:xyz/c:pqr[text()='12']",
          "namespaces": {
            "soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
            "b": "http://abc.example.com/b",
            "c": "http://abc.example.com/c"
          }
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "body": "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><b:abcOpernResponse xmlns:b=\"http://abc.example.com/b\"><b:result>Success - response1</b:result></b:abcOpernResponse></soapenv:Body></soapenv:Envelope>",
    "headers": {
      "Content-Type": "text/xml;charset=UTF-8"
    }
  }
}

兜底返回response2的Stub

{
  "request": {
    "method": "POST",
    "url": "/your-soap-endpoint" // 替换成实际接口路径
  },
  "response": {
    "status": 200,
    "body": "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><b:abcOpernResponse xmlns:b=\"http://abc.example.com/b\"><b:result>Default - response2</b:result></b:abcOpernResponse></soapenv:Body></soapenv:Envelope>",
    "headers": {
      "Content-Type": "text/xml;charset=UTF-8"
    }
  }
}

怎么用这个配置

  • 把这两个JSON文件放到WireMock的mappings目录里(独立版WireMock启动时会自动加载这个目录的配置)
  • 启动WireMock:java -jar wiremock-jre8-standalone-2.35.0.jar(替换成你下载的版本号就行)
  • 把SOAP请求发到http://localhost:8080/your-soap-endpoint,就能看到对应的响应了

几个要注意的点

  • 命名空间必须完全一致:XPath里的前缀和URI要和你的SOAP请求完全匹配,不然会匹配失败
  • 层级不能错:XPath的元素层级要和SOAP请求里的结构完全对应,比如示例里的a:Id是在a:abcHeaders下面,不能写错
  • 灵活调整条件:如果只需要校验Header或者Body其中一个,直接删掉对应的匹配规则就行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:25:28