如何匹配线程中的JSON,它具有相同的字段值,但可能不是相同的顺序

问题描述 投票:1回答:1

我已经请求JSON body [ {"name" : "Ram"}, {"name" : "Sam"} ]

这是我需要匹配请求的wiremock请求的输入,即使JSON具有相同的内容但值可能不是相同的顺序。例如, [ {"name" : "Sam"}, {"name" : "Ram"} ]

我使用的方法是.withRequestBody. I tried withequalToJson`但不起作用。什么是匹配仅检查JSON内容而不是订单的匹配器?

web-services request matching wiremock
1个回答
1
投票

这可以使用JsonPath解决,JsonPath是bodyPatterns相等匹配功能的一部分。

{
  "request" : {
    "urlPathPattern" : "/jpath/.*",
    "method" : "GET",
     "bodyPatterns" : [ {
      "matchesJsonPath" : "$[?(@.name == 'Sam')]"
    } ]   
  },
  "response" : {
    "status" : 200,
    "body" : "Works"
  }
}

使用JsonPath online evaluator可以轻松测试JsonPath表达式。有关可能的更多详细信息,请查看here

© www.soinside.com 2019 - 2024. All rights reserved.