最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Spring webflux flatmap callback is not executed - Stack Overflow

programmeradmin0浏览0评论

I have the following code inside a webfilter in my Srping webflux app

return this.authenticationConverter.convert(exchange)
            .onErrorResume(AuthenticationException.class, (ex) -> {
                this.logger.debug("Failed to process OIDC Back-Channel Logout", ex);
                return ex instanceof AuthenticationServiceException
                        ? Mono.error(ex)
                        : this.handleAuthenticationFailure(exchange, ex).then(Mono.empty());
            })
            .switchIfEmpty(Mono.defer(() -> {
                logger.debug("No authentication found, continuing filter chain.");
                return chain.filter(exchange);
            }).then(Mono.empty()))
            .flatMap(authenticationManager::authenticate)
            .onErrorResume(AuthenticationException.class, (ex) -> {
                this.logger.info("Failed to process OIDC Back-Channel Logout", ex);
                return ex instanceof AuthenticationServiceException
                        ? Mono.error(ex)
                        : this.handleAuthenticationFailure(exchange, ex).then(Mono.empty());
            })
            .flatMap(authentication -> {
                WebFilterExchange webFilterExchange = new WebFilterExchange(exchange, chain);
                logger.debug("Authentication successful, proceeding with logout.");
                return this.logoutHandler.logout(webFilterExchange, authentication);
            });

Although the code is executed successfully and reaches the line .flatMap(authentication -> {, the code inside the flatmap is never executed.

I feel that I have gotten something wrong about the reactive model, but don't know what.

I tried debugging (in Intellij IDEA), but the flatmap callback is always skipped. I am expecting the code inside the flatmap to be executed.

I have the following code inside a webfilter in my Srping webflux app

return this.authenticationConverter.convert(exchange)
            .onErrorResume(AuthenticationException.class, (ex) -> {
                this.logger.debug("Failed to process OIDC Back-Channel Logout", ex);
                return ex instanceof AuthenticationServiceException
                        ? Mono.error(ex)
                        : this.handleAuthenticationFailure(exchange, ex).then(Mono.empty());
            })
            .switchIfEmpty(Mono.defer(() -> {
                logger.debug("No authentication found, continuing filter chain.");
                return chain.filter(exchange);
            }).then(Mono.empty()))
            .flatMap(authenticationManager::authenticate)
            .onErrorResume(AuthenticationException.class, (ex) -> {
                this.logger.info("Failed to process OIDC Back-Channel Logout", ex);
                return ex instanceof AuthenticationServiceException
                        ? Mono.error(ex)
                        : this.handleAuthenticationFailure(exchange, ex).then(Mono.empty());
            })
            .flatMap(authentication -> {
                WebFilterExchange webFilterExchange = new WebFilterExchange(exchange, chain);
                logger.debug("Authentication successful, proceeding with logout.");
                return this.logoutHandler.logout(webFilterExchange, authentication);
            });

Although the code is executed successfully and reaches the line .flatMap(authentication -> {, the code inside the flatmap is never executed.

I feel that I have gotten something wrong about the reactive model, but don't know what.

I tried debugging (in Intellij IDEA), but the flatmap callback is always skipped. I am expecting the code inside the flatmap to be executed.

Share Improve this question edited Feb 2 at 15:08 Δημήτρης Τοπαλίδης asked Feb 2 at 15:08 Δημήτρης ΤοπαλίδηςΔημήτρης Τοπαλίδης 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

flatMap is an operator that transforms a value emitted asynchronously, usually "flatting" nested publishers.

In your case, the .then(Mono.empty())) call turns the emitted value into "empty". Since no value is emitted after that, the flatMap operator is never called. Maybe the order of operators isn't right here?

发布评论

评论列表(0)

  1. 暂无评论