简体   繁体   English

HTTP服务器响应404时未调用Spring WebClient异步回调

[英]Spring WebClient async callback not called when http server response 404

A problem I encountered is as titled, and the code is below: 我遇到的一个问题如标题所示,代码如下:

Mono<Account> accountMono = client.get()
    .uri("accounturl")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .flatMap(response -> {
                            if (response.statusCode().equals(HttpStatus.OK)) {
                              return response.bodyToMono(Account.class);
                            } else { 
                              return Mono.empty();
                            }
                          });

accountMono.subscribe(result -> callback(result));  

``` ```

Server response 404 . Server response 404 I try to create an empty Account, but the callback() is not called. 我尝试创建一个空帐户,但未callback() It looks like the empty Mono is not emitted. 好像没有发出空的Mono

Server response 404, I try to create a empty Account 服务器响应404,我尝试创建一个空帐户

You're not creating an empty Account . 您不是要创建一个空Account You're returning an empty Mono , ie a Mono that will never emit anything. 您将返回一个空的Mono ,即一个永远不会发出任何东西的Mono

If you want to return a Mono which emits an empty Account, then you need 如果您想返回一个发出空帐户的Mono,则需要

return Mono.just(new Account());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring RestTemplate,当HTTP状态码为404时得到垃圾响应 - Spring RestTemplate, getting junk response when http status code is 404 如何使用 Spring 5 Webclient 打印原始 HTTP 请求和 HTTP 响应? - How to print raw HTTP Request and HTTP Response with Spring 5 Webclient? Spring MVC HTTP 在服务器上运行时出现状态 404 错误 - Spring MVC HTTP Status 404 error when running on Server 如何在Spring WebClient / DataBuffer中拦截HTTP响应流量? - How to intercept http response traffic in Spring WebClient / DataBuffer? Spring WebClient - 链接异步调用 - Spring WebClient - chaining async calls 当响应正文中的某些字段不为 null 时,Spring WebClient 将它们设置为 null - Spring WebClient setting some fields to null when they are not null in the response body 如何记录Spring WebClient响应 - How to log Spring WebClient response Spring Boot MVC应用程序在部署到外部Tomcat / tc Server实例时返回HTTP 404 - Spring Boot MVC Application returns HTTP 404 when deployed to an external Tomcat/tc Server instance 当服务器提前响应时,Spring WebClient / Reactor-netty 坏了? - Spring WebClient / Reactor-netty broken when a server responds early? spring WebClient 如何从使用 http/1.1 的 spring webflux 服务器接收 stream 数据 - how spring WebClient can receive stream data from spring webflux server that is using http/1.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM