首页 > 解决方案 > Spring WebClient 连接被拒绝错误

问题描述

当我尝试使用 WebClient 发送 POST 请求时,出现以下错误。但是,如果我尝试使用邮递员将请求发送到同一个 uri,它是成功的。请帮助我解决这个问题,我陷入了这个问题我是 Spring WebFlux 的新手。

抛出异常[请求处理失败;嵌套异常是 reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection denied: localhost/127.0.0.1:8080] with root cause\n+ Throwable: java.net. ConnectException:finishConnect(..) 失败:在 io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) 处拒绝连接\n 在 io.netty.channel.unix.Socket.finishConnect(Socket.java :243)\n 在 io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672)\n 在 io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649)\ n 在 io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529)\n 在

我的 WebClient 代码发送 Post Req:

String success =
    webClient
        .post()
        .uri("/sendRequest")
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON)
        .headers(
            httpHeaders -> {
     
              httpHeaders.add(
                 headerName, headerValue);

            })
        .body(Mono.just(messageBody), String.class)
        .exchange()
        .flatMap(
            response -> {
              HttpStatus httpStatus = response.statusCode();
              if (httpStatus.is2xxSuccessful()) {
                System.out.println("Message posted");
              } else {
                System.err.println("Message FAILED. Status=" + httpStatus.toString());
              }
              return response.bodyToMono(String.class);
            })
        .block();

}

//我的 WebClient 生成器代码:

public WebClient getWebClient() {
  return WebClient.builder().baseUrl(this.MyUrl()).build();
}

标签: springspring-bootwebclientspring-webfluxspring-webclient

解决方案


推荐阅读