wiremock - Wiremock 独立的对象动态响应数组不适用于 bodyPatterns 和 matchesJsonPath
问题描述
我正在使用wiremock来存根请求。我创建了一个 json 文件来获得响应:
{
"request": {
"method": "POST",
"urlPath": "/nested/transform",
"bodyPatterns": [
{
"matchesJsonPath": "$.name.[0].first"
},
{
"matchesJsonPath": "$.name.[1].first"
}
]
},
"response": {
"status": 200,
"body": "{\"firstName\": \"$(name.[0].first)\", \"lastName\": \"$(name.[1].first)\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["body-transformer"]
}
}
我的请求和响应如下:
要求
{
"name": [
{
"first": "Vijay"
},
{
"first": "Sagar"
}
]
}
在这里,我收到了非常胡须的响应,并且没有按照我的意愿进行解析。
这不是我预期的结果:
{
"firstName": "[{first=Vijay}, {first=Sagar}]",
"lastName": "[{first=Vijay}, {first=Sagar}]"
}
预期结果是:我愿意根据上述请求和存根 json 收到以下响应:
{"firstName": "Vijay","lastName": "Sagar"}
当我尝试了很多但无法匹配响应参数时,如何获得预期的结果?
解决方案
在处理 JSON 响应时,我更喜欢使用bodyFileName,因为它不需要转义。
__files/nested_json_template.json
{
"firstName": "{{jsonPath request.body '$.name.[0].first'}}",
"lastName": "{{jsonPath request.body '$.name.[1].first'}}"
}
映射/nested_json_mapping.json
{
"request": {
"method": "POST",
"urlPath": "/nested/transform",
"bodyPatterns": [
{
"matchesJsonPath": "$.name.[0].first"
},
{
"matchesJsonPath": "$.name.[1].first"
}
]
},
"response": {
"status": 200,
"bodyFileName": "nested_json.json",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}
推荐阅读
- java - 我在返回两个数字的除法的java方法代码中有一个问题
- rest - 使用 REST API 2.0 重命名 BitBucket 存储库?
- c# - 试图找到如何从 Netgear Managed Switch 本地 UI 网页生成特定的 32 十六进制字符网络表单
- python - 如何在 python 中使用 azure 说话人识别 api
- python - Pandas 数据框列,其中包含不同列的不同长度的列表列表
- javafx - BorderPane 中的 JavaFX FileChooser
- python - Scrapy 返回 Null ([])
- angular - 使用 enterkey 提交多个表单
- javascript - 自定义 SingleValue 和 Option react-select - 显示选项,但不显示 SingleValue
- python - 一旦我的 tf2 模型开始训练,显示器就会变黑