【发布时间】:2021-02-10 08:37:14
【问题描述】:
我是 Spring Boot 框架的 WebClient 的新手。如何仅在错误时记录带有错误主体的 4xx/5xx,但在成功时将clientResponse 作为String 返回,我们以后可以使用gson 对其进行序列化?
String postResponse =
post(
ENDPOINT,
token,
request,
Request.class);
Response response = gson.fromJson(postResponse, Response.class);
private String post(String endpoint, String token, Mono<?> requestBody, Class<?> requestBodyClass) {
WebClient.Builder webClientBuilder = WebClient.builder();
WebClient.RequestBodySpec requestBodySpec = webClientBuilder.build().post().uri(uri);
requestBodySpec.header(
"Authorization", BEARER_TOKEN_PREFIX + this.accessTokens.get(token));
return requestBodySpec
.header("Content-Type", HEADER_VALUE_CONTENT_TYPE)
.body(Mono.just(requestBody), requestBodyClass)
.retrieve()
.bodyToMono(String.class)
.block();
}
【问题讨论】:
标签: java spring-boot spring-webclient