0

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

下面是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": "[]"
      }
    }
  ]
}

当我尝试Wiremock使用独立 jar ( wiremock-standalone-2.24.1.jar) 启动服务器时,我遇到了异常,我验证了 Json 模式,它看起来没问题。

Wiremock msrivast$ java -jar wiremock-standalone-2.24.1.jar 线程“main”com.github.tomakehurst.wiremock.standalone.MappingFileException 中的异常:加载文件/Users/msrivast/Downloads/Wiremock/./mappings/results 时出错.json:无法反序列化java.lang.String来自 com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:121) 的 START_ARRAY 令牌在 com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:199) 在 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) 在 com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65) 在 com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:125)

4

2 回答 2

1

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

响应定义中的正文键可以是body, jsonBody,base64Bodynull

于 2020-07-09T07:15:06.377 回答
0

body参数在 WireMock 中总是需要一个字符串。一旦我将正文中的响应作为字符串传递,它就起作用了。

于 2019-09-24T06:04:35.783 回答