【问题标题】:MockServer: IllegalArgumentException when parsing JSONMockServer:解析 JSON 时出现 IllegalArgumentException
【发布时间】:2018-08-24 05:33:13
【问题描述】:

当我尝试使用 MockServer 模拟外部 HTTP API 时,mockserver 返回 java.lang.IllegalArgumentException

这是测试代码:

new MockServerClient("localhost", 1080)
    .when(request("/messages")
    .withMethod("POST")
    .withQueryStringParameters(
        param("subject", "integration-test-subject")
    )
).respond(response().withStatusCode(200));

这是个例外:

java.lang.IllegalArgumentException: Exception while parsing 
[  
   {  
      "httpRequest":{  
         "method":"POST",
         "path":"/messages",
         "queryStringParameters":{  
            "subject":[  
               "integration-test-subject"
            ]
         }
      },
      "httpResponse":{  
         "statusCode":200
      },
      "times":{  
         "remainingTimes":0,
         "unlimited":true
      },
      "timeToLive":{  
         "unlimited":true
      }
   }
] for Expectation

这是杰克逊的例外:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of FIELD_NAME token
at
[  
   Source:(String)"   {  
      "httpRequest":{  
         "method":"POST",
         "path":"/messages",
         "queryStringParameters":{  
            "subject":[  
               "integration-test-subject"
            ]
         }
      },
      "httpResponse":{  
         "statusCode":200
      },
      "times":{  
         "remainingTimes":0,
         "unlimited":true
      },
      "timeToLive":{  
         "unlimited":true
      }
   }

我正在尝试使用正文发送application/x-www-form-urlencoded 请求

subject:integration-test-subject

.withQueryStringParameters(param("subject", "integration-test-subject")) 不存在于测试中时,一切正常。

如何解决这个问题?

【问题讨论】:

  • 任何解决方案了吗?似乎 MultiValueMap 的某些反序列化器未在杰克逊中注册。我有同样的问题,但有标题。
  • @PatrykDobrowolski 我还没有测试过,但看起来你应该将杰克逊降级为 com.fasterxml.jackson.core:jackson-databind:2.9.3

标签: jackson mockserver


【解决方案1】:

这是GitHub中的一个问题,有解释

https://github.com/jamesdbloom/mockserver/issues/451

你可以更新到 5.4.1

【讨论】:

    【解决方案2】:

    这个问题的解决方法是添加到你的项目中:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.3</version>
        </dependency>
    

    我的项目使用 Spring Boot 2 这个解决方案运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2018-04-25
      • 1970-01-01
      • 2018-08-11
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多