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



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

type1和type2都是enum字段,其他字段都是string字段。

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.*)

最新更新