Spring WebClient 连接被拒绝错误

问题描述

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

抛出异常[请求处理失败;嵌套异常是 reactor.core.Exceptions$ReactiveException:io.netty.channel.AbstractChannel$AnnotatedConnectException:finishConnect(..) 失败:连接被拒绝:localhost/127.0.0.1:8080] 与根本原因\n+ Throwable:java.net。 ConnectException:finishConnect(..) 失败:连接被拒绝\n 在 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.finishChannel49.java:672 n 在 io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529)\n 在

用于发送帖子请求的 WebClient 代码

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();
}

解决方法

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

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

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