使用Spring WebClient返回完整的响应

问题描述

我有以下代码

public ClientResponse doGet(HttpServletRequest request,URI uri) {
    return webClient.get()
            .uri(uri.toASCIIString())
            .headers(headers -> headers.putAll(processRequest(request))
            .exchange()
            .block();
}

但是当我尝试如下通过RestController返回此ClientResponse时,

@GetMapping
@ResponseBody
public ClientResponse doGet(HttpServletRequest request) {
    ClientResponse node = service.doGet(request);
    return node;
}

我收到以下错误:

org.springframework.http.converter.HttpMessageNotWritableException:否 找到类型为class的返回值的转换器 org.springframework.web.reactive.function.client.DefaultClientResponse

基本上,我想要的最终目标是将实际响应返回给调用者-包括标题,正文,cookies 等。

我当时正在考虑像下面那样使用ResponseEntity。

public ResponseEntity<JsonNode> doGet2(HttpServletRequest request,URI uri) {

    Mono<ClientResponse> clientResponse = webClient.get()
            .uri(uri.toASCIIString())
            .headers(headers -> headers.putAll(processRequest(request))
            .exchange();

    HttpHeaders headers = clientResponse.map(resp -> resp.headers().asHttpHeaders()).block();
    JsonNode body = clientResponse.flatMap(resp -> resp.bodyToMono(JsonNode.class)).block();

    ResponseEntity<JsonNode> jsonNode = ResponseEntity.ok().headers(headers).body(body);
    return jsonNode;
}

但这又是一个过大的杀伤力。有没有一种方法可以直接使用WebClient返回响应,而不是重构响应?

解决方法

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

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

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