java - 无法反序列化 Wiremock Json 文件配置中收到的 START_ARRAY token 异常中的 `java.lang.String` 实例

标签 java wiremock

我正在尝试使用带有 JSON 文件配置的 WireMock 独立 jar 来模拟一些 API。

下面是包含我正在使用的多个 stub 映射的 JSON 文件 -

{
  "mappings": [{
      "request": {
        "method": "GET",
        "url": "/users"
      },
      "response": {
        "status": 200,
        "body": [{
            "id": 1,
            "name": "Leanne Graham",
            "username": "Bret",
            "email": "Sincere@april.biz",
            "address": {
              "street": "Kulas Light",
              "suite": "Apt. 556",
              "city": "Gwenborough",
              "zipcode": "92998-3874",
              "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
              }
            },
            "phone": "1-770-736-8031 x56442",
            "website": "hildegard.org",
            "company": {
              "name": "Romaguera-Crona",
              "catchPhrase": "Multi-layered client-server neural-net",
              "bs": "harness real-time e-markets"
            }
          },
          {
            "id": 2,
            "name": "Ervin Howell",
            "username": "Antonette",
            "email": "Shanna@melissa.tv",
            "address": {
              "street": "Victor Plains",
              "suite": "Suite 879",
              "city": "Wisokyburgh",
              "zipcode": "90566-7771",
              "geo": {
                "lat": "-43.9509",
                "lng": "-34.4618"
              }
            },
            "phone": "010-692-6593 x09125",
            "website": "anastasia.net",
            "company": {
              "name": "Deckow-Crist",
              "catchPhrase": "Proactive didactic contingency",
              "bs": "synergize scalable supply-chains"
            }
          }
        ],

        "headers": {
          "Content-Type": "application/json"
        }
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/posts?userId=1"
      },
      "response": {
        "status": 200,
        "body": "[]"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/comments?postId=1"
      },
      "response": {
        "status": 200,
        "body": "[]"
      }
    }
  ]
}

当我尝试使用独立 jar (wiremock-standalone-2.24.1.jar) 启动 Wiremock 服务器时,出现以下异常,我验证了 Json 架构看起来还不错。

Wiremock msrivast$ java -jar wiremock-standalone-2.24.1.jar Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /Users/msrivast/Downloads/Wiremock/./mappings/results.json: Cannot deserialize instance of java.lang.String out of START_ARRAY token at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:121) at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:199) at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:195) at com.github.tomakehurst.wiremock.core.WireMockApp.(WireMockApp.java:103) at com.github.tomakehurst.wiremock.WireMockServer.(WireMockServer.java:72) at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65) at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:125)

最佳答案

基于代码库:https://github.com/tomakehurst/wiremock/blob/master/src/main/java/com/github/tomakehurst/wiremock/http/ResponseDefinition.java

响应定义中的 body 键可以是 bodyjsonBodybase64Bodynull

关于java - 无法反序列化 Wiremock Json 文件配置中收到的 START_ARRAY token 异常中的 `java.lang.String` 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58064761/

相关文章:

java - WireMock 失败并出现 NoSuchMethodError HttpServletResponse.getHeader

logging - 无法关闭 Wiremock 的调试日志记录

java - 如何访问 BufferedWriter 中的缓冲区

wiremock - 初始化 WireMock HTTP 服务器时出现 HttpHostConnectException

java - 使用 EC X509 证书加密电子邮件

java - 将数据非线性解析为不可变对象(immutable对象)

wiremock - 使用 WireMock 时查询参数未匹配

java - Wiremock 对于 stub url 返回 404

java - Criteria API 使用复杂的 where 子句创建查询

java - Apache 卢塞恩 : how to convert collection index to another format?