Spring WebFlux请求回调不起作用

huangapple go评论63阅读模式
英文:

Spring webflux request callback not working

问题

我正在使用 spring-webflux 5.1.7-RELEASE。我想在发送请求之前触发WebClient的回调。这是相关的代码:

ExchangeFilterFunction logRequest (SomeLogObject someLogObject) {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        /**
         业务逻辑回调在这里处理
         */
        return Mono.just(clientRequest);
    });
}

// 在某个其他地方插入logRequest回调的代码
WebClient webClient = WebClient
        .builder()
        .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
        .baseUrl(baseURL)
        .filters(exchangeFilterFunctions -> exchangeFilterFunctions.add(logRequest(someLogObject)))
        .build();

webClient
        .get()
        .uri(uriBuilder -> uriBuilder.path("some_uri_path").queryParams(queryParam).build())
        .header("some_header_key", "some_header_value")
        .retrieve().bodyToMono(String.class);

在这里,logRequest在一开始就被触发了(在请求触发之前)。根据我的调试,我发现它在调用retrieve()时触发。

是否有一种方法可以确保在发送请求之前立即触发logRequest,而不是在创建Mono时触发?

提前感谢您的帮助。

英文:

I am using spring-webflux 5.1.7-RELEASE. I want to trigger the callback for webclient just before the request is sent. The code for this is:

ExchangeFilterFunction logRequest (SomeLogObject someLogObject) {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        /**
         business logic for callback goes here
         */
        return Mono.just(clientRequest);
    });
}

//code for plugging in logRequest callback (at some othe place)
WebClient webClient = WebClient
        .builder()
        .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
        .baseUrl(baseURL)
        .filters(exchangeFilterFunctions -> exchangeFilterFunctions.add(logRequest(someLogObject)))
        .build();

webClient
        .get()
        .uri(uriBuilder -> uriBuilder.path("some_uri_path").queryParams(queryParam).build())
        .header("some_header_key", "some_header_value")
        .retrieve().bodyToMono(String.class);

Here the logRequest is triggered in beginning itself (much before the request is triggered). As per my debugging I found that it gets triggered when retrieve() is called.

Is there a way to ensure that logRequest gets triggered immediately before the request is sent and NOT when the mono is created?

Thanks in advance

答案1

得分: 2

我认为您正在寻找以下更改:

ExchangeFilterFunction logRequest(SomeLogObject someLogObject) {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> Mono.defer(() -> {
        /**
         * 回调的业务逻辑在此处
         */
        return Mono.just(clientRequest);
    }));
}

Mono.defer() 会延迟代码执行直到实际请求。

英文:

I think you are looking for the following change:

ExchangeFilterFunction logRequest (SomeLogObject someLogObject){
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest ->
            Mono.defer(() -> {
                /**
                 business logic for callback goes here
                 */
                return Mono.just(clientRequest);
            })
    );
}

Mono.defer() will delay code execution until the real request.

huangapple
  • 本文由 发表于 2020年1月6日 17:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定