小编sit*_*000的帖子

Spring WebClient 获取请求返回“连接被拒绝:localhost/”

当我尝试使用 WebClient 发送 GET 请求时,我收到错误 io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection returned: localhost/ 的响应

* Web客户端初始化 *

//hostname = "localhost/127.0.0.1:8081"
//contextPath = "user-service"

    @Bean
    public WebClient userWebclient(@Value("${service.user.hostname}") final String hostname,
                                  @Value("${service.user.contextPath}") final String contextPath) {
        return WebClient
                .builder()
                .baseUrl(hostname + contextPath)
                .build();
    }
Run Code Online (Sandbox Code Playgroud)

* 控制器 *

    private final WebClient userWebclient;

    @GetMapping(value = "/user/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Mono<String> getUserById(@NotNull @PathVariable String userId) {
        userWebclient.

        return userWebclient.get()
               .uri("/UserMock/user/" + userId)
               .header("Accept", "application/json")
               .header("Authorization", "Bearer =........")
               .exchange()
               .flatMap(response -> response.bodyToMono(String.class));
    }
Run Code Online (Sandbox Code Playgroud)

错误信息

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8081 …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-webflux

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

spring-boot ×1

spring-webflux ×1