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

如何捕获SOAPui发往Wiremock Server的XML报文并与XSD校验

嘿,我来帮你搞定这个问题!Wiremock其实有好几种实用的方式能捕获到SOAPui发过来的XML请求,之后对接XSD校验也没那么复杂,咱们一步步拆解来看:

一、用Wiremock的存根(Stub)捕获请求

Wiremock的核心能力就是通过存根匹配并记录请求,你可以创建一个存根来接收所有SOAP请求,同时把请求体保存下来:

  • 如果你习惯用JSON配置存根,可以这么写:
    {
      "request": {
        "method": "POST",
        "urlPattern": "/your-soap-endpoint", // 匹配你的SOAP接口路径
        "headers": {
          "Content-Type": {
            "equalTo": "application/xml" // 指定XML类型的请求头,确保正确匹配
          }
        }
      },
      "response": {
        "status": 200,
        "body": "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body></soap:Body></soap:Envelope>" // 返回一个极简的SOAP响应即可
      },
      "recordRequest": true // 关键!开启请求记录开关
    }
    
  • 要是用Java代码配置存根,写法是这样:
    stubFor(post(urlEqualTo("/your-soap-endpoint"))
            .withHeader("Content-Type", equalTo("application/xml"))
            .willReturn(aResponse().withStatus(200).withBody("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body></soap:Body></soap:Envelope>"))
            .withCaptureHeader("Content-Type", containing("application/xml")));
    
  • 配置完成后,Wiremock会把捕获到的请求默认存在__files/mappings/requests目录下,你直接从这些文件里就能提取出XML请求体。
二、用自定义扩展实现捕获+实时XSD校验

如果想要更灵活的处理(比如捕获请求后立刻做XSD校验),可以写一个Wiremock扩展,实现RequestListener接口:

  • 先写一个自定义监听器,把捕获和校验逻辑整合进去:
    import com.github.tomakehurst.wiremock.core.Admin;
    import com.github.tomakehurst.wiremock.extension.Extension;
    import com.github.tomakehurst.wiremock.extension.PostServeAction;
    import com.github.tomakehurst.wiremock.http.Request;
    import com.github.tomakehurst.wiremock.http.Response;
    import javax.xml.XMLConstants;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    import java.io.File;
    import java.io.StringReader;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    public class XmlCaptureValidationExtension implements PostServeAction {
        private final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    
        @Override
        public String getName() {
            return "xml-capture-validation";
        }
    
        @Override
        public void doAction(Request request, Response response, Admin admin) {
            // 提取XML请求体
            String xmlBody = request.getBodyAsString();
            try {
                // 加载你的XSD文件
                Schema schema = schemaFactory.newSchema(new File("path/to/your/bookstore.xsd"));
                Validator validator = schema.newValidator();
                // 执行校验
                validator.validate(new StreamSource(new StringReader(xmlBody)));
                System.out.println("✅ XML请求符合XSD规范!");
                // 把捕获到的XML保存到本地文件
                Files.write(Paths.get("captured-soap-request.xml"), xmlBody.getBytes());
            } catch (Exception e) {
                System.err.println("❌ XML校验失败:" + e.getMessage());
            }
        }
    }
    
  • 启动Wiremock的时候注册这个扩展:
    WireMockServer wireMockServer = new WireMockServer(options()
            .port(8080)
            .extensions(new XmlCaptureValidationExtension()));
    wireMockServer.start();
    
    这样每次SOAPui发请求过来,监听器都会自动捕获XML内容、完成XSD校验,还能把请求体保存到指定文件。
三、用Wiremock的录制功能批量捕获请求

如果你只是想先批量捕获请求、之后再处理,Wiremock的录制功能会很省心:

  • 用命令行启动Wiremock并开启录制模式(指定录制目录):
    java -jar wiremock-jre8-standalone.jar --record-mappings --proxy-all="http://localhost:8080" --recorded-requests-dir="./captured-soap-requests"
    
  • 接着用SOAPui往Wiremock发送请求,所有请求都会被录制下来,存到指定目录的JSON文件里,你直接从中提取XML请求体即可。
额外小提示

记得确保请求的Content-Type头是application/xml或者text/xml,这样Wiremock能正确识别并解析XML请求体,避免出现匹配失败或者解析错误的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:02:57