微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Cloud Contract 消费者端测试返回 404

如何解决Spring Cloud Contract 消费者端测试返回 404

使用存根在消费者端运行 Spring Contract 测试时。我首先得到以下响应以创建端点,然后是发送给它的请求,然后是响应。

type1 和 type2 都是枚举字段,其他字段都是字符串。

127.0.0.1 - POST /mappings

Connection: [keep-alive]
User-Agent: [Apache-HttpClient/4.5.12 (Java/11.0.5)]
Host: [localhost:11291]
Content-Length: [834]
Content-Type: [text/plain; charset=UTF-8]
{
  "id" : "7d0b6496-be88-43c0-891d-bdb8ef8ae033","request" : {
    "url" : "/testEndpoint","method" : "PUT","headers" : {
      "Content-Type" : {
        "matches" : "application/json;charset=UTF-8.*"
      }
    },"bodyPatterns" : [ {
      "matchesJsonPath" : "$[?(@.['name'] == 'Test')]"
    },{
      "matchesJsonPath" : "$[?(@.['type1'] == 'NONE')]"
    },{
      "matchesJsonPath" : "$[?(@.['type2'] == 'NONE')]"
    },{
      "matchesJsonPath" : "$[?(@.['description'] == null)]"
    },{
      "matchesJsonPath" : "$[?(@.['comment1'] == null)]"
    },{
      "matchesJsonPath" : "$[?(@.['comment2'] == null)]"
    } ]
  },"response" : {
    "status" : 201,"transformers" : [ "response-template" ]
  },"uuid" : "7d0b6496-be88-43c0-891d-bdb8ef8ae033"
}

127.0.0.1 - PUT /testEndpoint

Connection: [keep-alive]
User-Agent: [Apache-HttpClient/4.5.12 (Java/11.0.5)]
Host: [localhost:11291]
Accept-Encoding: [gzip,deflate]
Content-Length: [117]
Content-Type: [application/json; charset=UTF-8]
{"name":"Test","type1":"NONE","type2":"NONE","description":null,"comment1":null,"comment2":null}

我得到的回应是:

Matched response deFinition:
(no response deFinition configured)

Response:
HTTP/1.1 404
(no headers)

解决方法

实际上我看到问题是json内容类型的正则表达式是“application/json;charset=UTF-8.*”而实际是“application/json;charset=UTF-8”(注意空格)。您应该更改合同以支持该空间的正则表达式(例如 application/json.*

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。