这是不可能的retrieve
。您需要使用该exchange
功能而不是retrieve
,
webClient.post()
.uri(host)
.syncBody(req)
.exchange()
.flatMap(response -> {
return response.bodyToMono(MyResponse.class).map(myResponse -> {
List<String> headers = response.headers().header("session-id");
// here you build your new object with the response
// and your header and return it.
return new MyNewObject(myResponse, headers);
})
});
}).block();
class MyResponse {
// object that maps the response
}
class MyNewObject {
// new object that has the header and the
// response or however you want to build it.
private String httpHeaderSessionId;
private MyResponse myResponse;
}
网络客户端交换
或使用可变对象:
...
.exchange()
.flatMap(rsp -> {
String id = rsp.headers().asHttpHeaders().getFirst("session-id");
return rsp.bodyToMono(MyResponse.class)
.doOnNext(next -> rsp.setHttpHeaderSessionId(id));
})
.block();