简体   繁体   English

Spring Webflux webclient问题,尝试发送post请求时没有任何反应

[英]Issue with Spring Webflux webclient , nothing happens when trying to send post request

Have the following implementation of webclient : 有以下webclient实现:

    public <T> WebClient.ResponseSpec sendRequest(HttpMethod method, String contentType, T body, String baseUrl, String path) {
    try {
        WebClient webClient = WebClient.builder().baseUrl(baseUrl).filter(logRequest()).build();
        WebClient.ResponseSpec responseSpec = webClient.method(method)
                .uri(path)
                .header(HttpHeaders.CONTENT_TYPE, contentType)
                .body(BodyInserters.fromObject(body))
                .retrieve();
        return responseSpec;
    } catch (Exception e) {
        throw new WebClientProcessingException("Exception when trying to execute request", e);

    }
}

// This method returns filter function which will log request data
private static ExchangeFilterFunction logRequest() {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        LOGGER.info("Request: {} {} {}", clientRequest.method(), clientRequest.url(), clientRequest.body());
        clientRequest.headers().forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
        return Mono.just(clientRequest);
    });
}

Also have the following code , which is creating user object and command which contains user object , then calling webclient to send an request 还有以下代码,即创建包含用户对象的用户对象和命令,然后调用webclient发送请求

@Autowired
private BaseWebClient baseWebClient;
@Override
public void saveOrUpdateUser() {
        UserPayload userPayload = new UserPayload();
        userPayload.setUserId(111L);
        userPayload.setCreatedAt(ZonedDateTime.now(DateTimeProps.systemTimeZone));
        UserCommand userCommand = new UserCommand();
        userCommand.setUser(userPayload);
        baseWebClient.sendRequest(HttpMethod.POST, "application/json",
            Stream.of(userCommand).collect(Collectors.toList()),
            "http://localhost:8080",
            "/users").onStatus(HttpStatus::isError, clientResponse -> {
        throw new WebClientUserSaveOrUpdateeFailedException("Exception when trying to update user state")
        .bodyToMono(String.class);
    });
}

User payload : 用户负载:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserPayload {
  Long userId;
  ZonedDateTime createdAt;
}

User command : 用户命令:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserCommand {
     @JsonProperty("user")
     UserPayload user;
}

Json which is waiting for my other app (whom I am sending a request) : Json正在等待我的其他应用程序(我发送请求的人):

[
  { "user":
            { 
              "userId": 1,
              "createdAt": "2019-05-16T08:24:46.412Z"
            } 
  }
]

Using : Spring boot 2 , Lombok (for getter/setter) , gradle When I'm trying to send a request nothing happens. 使用:Spring boot 2,Lombok(用于getter / setter),gradle当我尝试发送请求时没有任何反应。 No exception even. 甚至没有例外。 I tried with very simple case as well the same issue. 我尝试了非常简单的案例以及相同的问题。 One more note, is it possible to log body? 还有一点需要注意,是否可以记录身体? I mean somehow see final json I guess I am missing something general. 我的意思是以某种方式看到最后的json我想我错过了一般的东西。

In Reactor, nothing happens until you subscribe . 在Reactor中, 在您订阅之前没有任何反应。 retrive() does not actually start the request. retrive()实际上并不启动请求。 As you can see in the example , you should use one of the method to convert the ResponseSpec to a Publisher and then eventually subscribe to that publisher. 正如您在示例中所看到的,您应该使用其中一种方法将ResponseSpec转换为发布服务器,然后最终订阅该发布者。

Depending on how you're using this method, you might be able to let Spring subscribe to the publisher instead. 根据您使用此方法的方式,您可以让Spring订阅发布者。 WebFlux supports reactive types in the model which means you can directly return a Mono from your RestController methods, for example. WebFlux支持模型中的反应类型,这意味着您可以直接从RestController方法返回Mono

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

相关问题 尝试在春季使用 webClient 在发布请求中发送多个字符串列表 - Trying to send multiple lists of Strings in post request using webClient in spring Java Spring Webflux WebClient - 使用 HTTP2 发送 http 请求 - Java Spring Webflux WebClient - send http request using HTTP2 Spring Webflux WebClient将文件发布到客户端 - Spring webflux WebClient post a file to a client Spring Webflux Webclient:窃听记录问题 - Spring Webflux Webclient: Issue with wiretap logging 如何通过Java Spring Webflux发送多个POST请求 - How to Send multiple POST request through Java Spring Webflux 尝试转换 bodyToMono 时,Spring webclient 抛出不受支持的媒体类型异常<uuid> , POST 请求</uuid> - Spring webclient throwing unsupported mediatype exception when trying to convert bodyToMono<UUID>, POST request 如何使用Spring webflux将实时进度发送到webclient? - How to send real time progress to webclient using Spring webflux? Spring WebFlux,如何调试我的WebClient POST交换? - Spring WebFlux, how can I debug my WebClient POST exchange? Spring WebFlux WebClient - 如何解决 400 错误请求 - Spring WebFlux WebClient - How to resolve 400 Bad Request 如何在Spring Webflux WebClient中发送“忘却”请求? - How make “fire and forget” request sending in spring webflux webclient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM