如何在WireMock中从请求JSON提取archiveId和amount到响应JSON
WireMock 动态映射请求字段到响应的配置方法
原请求匹配配置
{ "request":{ "method" : "POST", "urlPattern" :"/payments/v1/payments", "headers":{ "x-session-id":{ "matches":"[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}" }, "x-request-id":{ "matches":"[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}" }, "x-api-key":{ "matches":"(?s).*" }, "Content-Type":{ "equalTo":"application/json" }, "Accept":{ "equalTo":"application/json" }, "x-application-id":{ "matches":"(?s).*" }, "X-Client-Channel":{ "equalTo":"client" }, "Authorization":{ "matches":"Bearer [a-zA-Z0-9&._-]{1,}" }, "context":{ "equalTo":"Pankin ostopolku" } }, "bodyPatterns": [ {"matchesJsonPath": "$[?(@.isRetry == false)]"}, {"matchesJsonPath": "$[?(@.debtorAccount.IBAN == null)]"}, {"matchesJsonPath": "$[?(@.debtorAccount.NotApplicable == 'NOT_APPLICABLE')]"}, {"matchesJsonPath": "$[?(@.creditorAccount.IBAN =~ /.*/)]"}, {"matchesJsonPath": "$[?(@.creditorAccount.NotApplicable == null)]"}, {"matchesJsonPath": "$[?(@.creditorTransactionCode == '500')]"}, {"matchesJsonPath": "$[?(@.instructedAmount.amount =~ /.*/)]"}, {"matchesJsonPath": "$[?(@.instructedAmount.currency == 'EUR')]"}, {"matchesJsonPath": "$[?(@.intermediaryAccount == '3531800')]"}, {"matchesJsonPath": "$[?(@.ownMessage == null)]"}, {"matchesJsonPath": "$[?(@.archiveId =~ /.*/)]"}, {"matchesJsonPath": "$[?(@.paymentType =~ /.*/)]"} ] } }
示例请求JSON
{ "isRetry": false, "debtorAccount": { "IBAN": null, "NotApplicable": "NOT_APPLICABLE" }, "creditorAccount": { "IBAN": "FI2052091920276802", "NotApplicable": null }, "creditorTransactionCode": "500", "instructedAmount": { "amount": "57000.00", "currency": "EUR" }, "intermediaryAccount": "3531800", "ownMessage": null, "archiveId": "202311235SCL00000026", "paymentType": "CREDIT_ONLY" }
示例响应JSON(目标格式)
{ "debtorAccount": { "NotApplicable": null }, "additionalId": "0", "creditorAgent": {}, "orderReceipt": "false", "needsSplit": "false", "transactionCode": null, "transactionDate": "2023-11-06", "ownMessage": null, "archiveId": "202311235SCL00000026", "transactionId": "59986920448925_2023-11-06_202311235SCL00000026_0", "paymentType": "CREDIT_ONLY", "paymentId": "A_59986920448925_2023-11-06_202311235SCL00000026_0", "creditorAccount": { "IBAN": null }, "instructedAmount": { "amount": "57000.00", "currency": "EUR" }, "bookingDate": "2023-11-06", "status": "PROCESSED", "modifications": {} }
需求说明
需要把请求JSON中的archiveId和instructedAmount.amount字段值,自动填充到响应JSON的对应字段中,实现动态响应。
实现步骤
1. 启用WireMock的响应模板功能
WireMock的Response Templating是实现动态响应的核心功能,启用方式二选一:
- 命令行启动WireMock时添加参数:
--enable-response-templating - 在Stub的JSON配置中,响应部分添加
"transformers": ["response-template"]
2. 修改Stub配置,添加动态响应模板
在原请求匹配配置的基础上,新增response节点,用{{jsonPath}}表达式从请求体中提取目标字段值,替换响应中的对应位置:
{ "request":{ // 原请求匹配配置保持不变 "method" : "POST", "urlPattern" :"/payments/v1/payments", "headers":{/* 原header匹配规则 */}, "bodyPatterns": [/* 原body匹配规则 */] }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "transformers": ["response-template"], "jsonBody": { "debtorAccount": { "NotApplicable": null }, "additionalId": "0", "creditorAgent": {}, "orderReceipt": "false", "needsSplit": "false", "transactionCode": null, "transactionDate": "2023-11-06", "ownMessage": null, "archiveId": "{{jsonPath request.body '$..archiveId'}}", "transactionId": "59986920448925_2023-11-06_{{jsonPath request.body '$..archiveId'}}_0", "paymentType": "CREDIT_ONLY", "paymentId": "A_59986920448925_2023-11-06_{{jsonPath request.body '$..archiveId'}}_0", "creditorAccount": { "IBAN": null }, "instructedAmount": { "amount": "{{jsonPath request.body '$..instructedAmount.amount'}}", "currency": "EUR" }, "bookingDate": "2023-11-06", "status": "PROCESSED", "modifications": {} } } }
关键说明
{{jsonPath request.body '$..archiveId'}}:通过JSONPath表达式从请求体中提取archiveId字段的值{{jsonPath request.body '$..instructedAmount.amount'}}:提取请求中instructedAmount.amount的值- 不仅可以替换单独字段,还能像
transactionId这样把提取的值嵌入到字符串中,实现更灵活的动态内容
内容的提问来源于stack exchange,提问作者Shruti Mishra
相关产品推荐
相关产品推荐

