Wiremock 为春云合约存根运行器返回错误的内容类型



我是第一次尝试春云合约。我正在尝试让我的客户端自动发现合约存根,但即使我的合约在响应中指定了内容类型"application/json",我从 WireMock 获得的内容类型为"application/octet"。我做错了什么?

我的服务中有一个简单的方法,它从/status端点返回这样的模型:

{
"name": string,
"status": string
}

我的合同看起来像这样:

import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method('GET')
headers {
contentType(applicationJson())
}
url("/status")
}
response {
status OK()
body(
name: "Demo",
status: "RUNNING"
)
headers {
contentType(applicationJson())
}
}
}

在我的客户端中,我有一个使用 SpringRestTemplate来查询此端点的类:

@Component
public class StatusClient {
private final RestTemplate restTemplate;
public StatusClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public Status getStatus() {
return this.restTemplate
.exchange("http://localhost:8080/status", HttpMethod.GET, null, Status.class)
.getBody();
}
}
@Data
class Status implements Serializable {
private String name;
private String status;
}

我的单元测试使用@AutoConfigureStubRunner从本地存储库中提取最新版本的合约,并断言合约的响应(例如名称 = Demo,状态 = RUNNING(。

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = {"com.example:contract-demo:+:8080"}, stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class StatusClientTests {
@Autowired
private StatusClient client;
@Test
public void testThatStatusReturnsSuccessfully() {
Status result = this.client.getStatus();
assertEquals("Demo", result.getName());
assertEquals("RUNNING", result.getStatus());
}
}

当我运行测试时,WireMock 按预期报告它收到的合同:

2018-05-31 11:36:49.919  INFO 14212 --- [tp1255723887-26] WireMock                                 : Request received:
127.0.0.1 - GET /status
User-Agent: [Java/1.8.0_161]
Connection: [keep-alive]
Host: [localhost:8080]
Accept: [application/json, application/*+json]

Matched response definition:
{
"status" : 200,
"body" : "{"name":"Demo","status":"RUNNING"}",
"headers" : {
"contentType" : "application/json"
},
"transformers" : [ "response-template" ]
}
Response:
HTTP/1.1 200
contentType: [application/json]

但是当 RestTemplate 尝试反序列化它时,它会引发异常,因为一旦它命中提取数据的方法,响应内容类型实际上是"应用程序/八位字节":

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.contractclientdemo.Status] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:119)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:991)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:974)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)
at com.example.contractclientdemo.StatusClient.getStatus(StatusClient.java:18)

我正在使用Finchley.RC2作为Spring云版本,spring-cloud-starter-contract-stub-runner是我除了spring-boot-starter-test之外唯一的测试依赖项。

我知道 WireMock 返回了错误的内容类型,因为我在 Spring 中深入调试了 HttpMessageConverterExtractor 类,这就是getContentType方法在查询时返回的内容。

为什么 WireMock 返回错误的内容类型,尽管它在日志中报告了正确的内容类型?我怎样才能让它正确返回一个application/json以便我可以反序列化我的简单消息?

我遇到了和你完全相同的问题。我通过添加解决了它

headers {
header 'Content-Type': 'application/json;charset=UTF-8'
}

到响应。 你的回应中似乎有,虽然是用另一种方式写的,但这确实解决了我的问题。所以这与它有关。

在进行更改之前,curl 没有显示内容类型响应标头:

curl -v -H "接受:应用程序/json" 本地主机:6565/产品/ABC
* 正在尝试 127.0.0.1...
* TCP_NODELAY设置
* 连接到本地主机 (127.0.0.1( 端口 6565 GET/products/ABC HTTP/1.1
主机: 本地主机:6565

用户代理: curl/7.58.0
接受:应用程序/json

HTTP/1.1 200 OK
传输编码:分块
服务器:Jetty(9.2.z-SNAPSHOT(

与主机本地主机的连接 #0 保持不变

{  
"price": {  
"currencyCode": "EUR",  
"value": "100.50"  
},  
"name": "Fake product"  
}

完成更改后,curl 返回了这个,RestTemplate 设法反序列化它。

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
传输编码:分块
服务器:码头(9.2.z-快照(

这是我的工作合同:

import org.springframework.cloud.contract.spec.Contract
Contract.make {
description "should return product information"
request{
method GET()
url("/products/ABC")
}
response {
status 200
headers {
header 'Content-Type': 'application/json;charset=UTF-8'
}
body([
name: 'Fake product',
price:[
currencyCode: 'EUR',
value: 100.50
]
])
}
}

希望这有帮助

我认为你应该把它作为一个问题在WireMock中提交。此外,您没有在请求显式中设置应用程序/json 内容类型标头。也许这是一个问题?它也不应该是内容类型作为响应存根中的标头名称吗?

最新更新