Webclient响应处理程序(成功和错误)均未针对空的响应主体执行

时间:2018-08-13 14:06:52

标签: spring-webflux

具有使用OkHttp实现的异步REST API客户端,效果很好。 出于好奇,尝试将其转换为WebClient,观察到奇怪的行为。

WebClient配置就是这样:

webClient = WebClient.builder()
    .defaultHeaders(headers -> headers.add(HttpHeaders.CONTENT_TYPE,
        org.springframework.http.MediaType.APPLICATION_JSON_VALUE))
    .clientConnector(new ReactorClientHttpConnector(builder -> builder
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout)))
    .build();

请求代码:

void getTokenWithWebClient(Consumer<Try<String>> callback) {
    webClient.post()
        .uri(url)
        .syncBody(new MyRequest())
        .retrieve()
        .onStatus(status -> status.value() != HttpStatus.OK.value(),
            rs -> rs.bodyToMono(String.class).map(body -> new IOException(String.format(
                "Response HTTP code is different from 200: %s, body: '%s'", rs.statusCode(), body))))
        .bodyToMono(MyResponse.class)
        .subscribe(rs -> callback.accept(Try.of(() -> validateResponse(Option.of(rs)))),
            ex -> callback.accept(Try.failure(ex)));
}

在单元测试中,作为参数传递给此方法的回调完成了Future,我在上面等待。 因此,当我运行在IDEA中进行测试,并且请求导致响应的主体为空(内容长度:0)时,subscribe()中的lambda永远不会执行-使用println调试进行了验证。 / p>

但是,当我调试相同的测试时,即使未设置任何断点,它也会按预期完成,并根据结果调用lambda。

我确实在日志中看到了这一点:

[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClientOperations - [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522] Received last HTTP packet
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522] USER_EVENT: [Handler Terminated]
[reactor-http-nio-4] DEBUG reactor.ipc.netty.channel.ChannelOperationsHandler - [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522] Disposing context reactor.ipc.netty.channel.PooledClientContextHandler@3547abe3
[reactor-http-nio-4] DEBUG reactor.ipc.netty.channel.PooledClientContextHandler - Releasing channel: [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522]
[reactor-http-nio-4] DEBUG reactor.ipc.netty.resources.DefaultPoolResources - Released [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522], now 0 active connections
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522] READ COMPLETE
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 - R:localhost/127.0.0.1:58522] READ COMPLETE
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 ! R:localhost/127.0.0.1:58522] CLOSE
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 ! R:localhost/127.0.0.1:58522] INACTIVE
[reactor-http-nio-4] DEBUG reactor.ipc.netty.http.client.HttpClient - [id: 0xeffaded6, L:/127.0.0.1:49265 ! R:localhost/127.0.0.1:58522] UNREGISTERED

但是后来我的错误的Mono卡在了某个地方。

在Windows 7,Oracle JDK8 x64,IDEA 2018上运行此命令。 选项/尝试是vavr类(io.vavr:vavr),与这种情况无关。 对于单元测试,我使用Ratpack模拟有问题的REST API。

尝试使用exchange()而不是restore()并在没有onStatus()的情况下检查subscribe()lambda中的状态码-结果相同。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

长话短说,rs.bodyToMono(String.class).defaultIfEmpty("")挽救了一天

相关问题