在问这个问题之前,我已经尝试过使用:
log.error("fail {} ", ex.getResponseBodyAsString());
我已经将
.defaultStatusHandler()
附加到restClient 上。这在调试或记录错误时并没有真正的帮助。我想要的是一个清晰易读的 httpResponse,就像我们在 Postman 上看到的那样。
目前,我在做:
aRestClient.post()
.uri(turl)
.body(request)
.retrieve()
.onStatus(httpStatusCode -> httpStatusCode.value()== 400, (req, res) -> {
System.out.printf(Arrays.toString(res.getBody().readAllBytes()));
})
.body(LivinPointConvertResponse.class);
产量:
[123, 34, 99, 111, 100, 101, 34, 58, 49, 53, 44, 34, 109, 101, 115, 115, 97, 103, 101, 83, 116, 114, 34, 58, 34, 77, 97, 97, 102, 44, 32, 74, 117, 109, 108, 97, 104, 32, 109, 105, 110, 105, 109, 117, 109, 32, 112, 101, 110, 117, 107, 97, 114, 97, 110, 32, 54, 32, 76, 105, 118, 105, 110, 39, 112, 111, 105, 110, 34, 44, 34, 115, 117, 99, 99, 101, 115, 115, 34, 58, 102, 97, 108, 115, 101, 44, 34, 100, 97, 116, 97, 34, 58, 110, 117, 108, 108, 125]
您应该执行
new String()
而不是 Arrays.toString
来检索可读的响应。
如果我这样做,我可以重现您的用例:
restClient.post().uri("/posts").contentType(MediaType.APPLICATION_JSON).body(post)
.retrieve()
.onStatus(httpStatusCode -> httpStatusCode.value()== 400, (req, res) -> {
System.out.printf(Arrays.toString(res.getBody().readAllBytes()));
})
.toBodilessEntity();
System.out.println();
restClient.post().uri("/posts").contentType(MediaType.APPLICATION_JSON).body(post)
.retrieve()
.onStatus(httpStatusCode -> httpStatusCode.value()== 400, (req, res) -> {
System.out.printf(new String(res.getBody().readAllBytes()));
})
.toBodilessEntity();
我的输出是:
[123, 34, 116, 105, 116, 108, ..., 125]
{"title":"title1","content":"content1","status":"DRAFT"}