我想在我的项目中使用 Spring Boot WebClient 来访问 REST-API。第一个请求执行 REST-API 登录并接收 cookie 作为响应。此 cookie 用作所有其他请求的“API 令牌”。
然而,WebClient似乎不存储cookie。我必须自己做吗?如果是这样,最好的方法是什么?
以下是我的示例源代码。第二个请求不使用第一个请求的 cookie。
// ### 1. API-Call to get the cookie ###
ClientResponse clientResponse = webClient
.get()
.uri("/api/")
.headers(headers -> headers.setBasicAuth(user,passwd)
.exchange()
.block();
// ### Debug Cookie ###
if (clientResponse.statusCode().is2xxSuccessful()) {
for (String key : clientResponse.cookies().keySet()) {
LOG.debug("key:" + key + " value: " + clientResponse.cookies().get(key).get(0).getValue() );
}
}
// ### 2.API-Call (with cookie) ###
webClient
.get()
.uri("/api/document/4711")
.retrieve()
.onStatus(HttpStatus::is2xxSuccessful, r -> {
for (String key : …Run Code Online (Sandbox Code Playgroud)