为什么在 Spring Boot WebClient 中向日志响应正文添加响应过滤器会导致 http 异常?

问题描述

我正在尝试记录从 webclient 调用接收到的响应正文。这是我的网络客户端的样子:

  UserInfoResponse userInfoResponse = webClientBuilder
                .baseUrl(baseUrl)
                .filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
                    return clientResponse.bodyToMono(String.class).flatMap(body -> {
                        log.info("Userinformation Response received: [ {},{} ]",body,uuid);
                        return Mono.just(clientResponse);
                    } );
                }))
                .filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse -> Mono.just(clientResponse.mutate()
                        .headers(httpHeaders -> httpHeaders.remove(HttpHeaders.CONTENT_TYPE))
                        .headers(httpHeaders -> httpHeaders.setContentType(MediaType.APPLICATION_JSON))
                        .build()
               )))
                .build()
                .post()
                .uri("/getUserInfo")
                .headers(httpHeaders -> httpHeaders.setContentType(MediaType.APPLICATION_JSON))
                .headers(httpHeaders -> httpHeaders.setBearerAuth(token))
                .body(BodyInserters.fromValue(userInfoRequest))
                .exchangetoMono(clientResponse -> {
                    if(clientResponse.statusCode().is2xxSuccessful()){
                        return clientResponse.bodyToMono(UserInfoResponse.class);
                    }
                    log.error("Http failure on getUserinformation: [ {},clientResponse.statusCode(),uuid);
                    throw new Exception(clientResponse.statusCode().toString(),message);
                })
                .block();

当我使用该 Web 客户端运行 http 调用时,出现以下异常:

java.lang.IllegalStateException: The underlying HTTP client completed without emitting a response.

当响应正文成功打印时。但是当我删除一个过滤器(responseBody 日志记录)或所有过滤器时,不会抛出异常。

有人可以解释为什么会发生这种情况吗? 注意:Java 响应式编程的新手。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)