1

Below code compiles fine:

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    ........

 .flatMap(user-> {
            userRepository.save(user).subscribe();
            return chain.filter(exchange);
  });

......

chain.filter(exchange) whose return type is Mono(Void) delegates to the next web filter in the chain.

But I need to invoke the line return chain.filter(exchange); after successful completion of userRepository.save otherwise it fails to save the user when next webfilter runs.

I tried below code but it does not even compile.

.flatMap(user-> {
userRepository.save(user).subscribe(u -> chain.filter(exchange) );

}); 

How to fix this problem?

ace
  • 11,526
  • 39
  • 113
  • 193

1 Answers1

1

Try this:

return
...
.flatMap(userRepository::save)
.then(chain.filter(exchange));

1) Combine calling filter and userRepository to one chain;

2) If userRepository::savecaused error, data emits will be stopped and request aborted, otherwise then() will call chain.filter(exchange).

Yauhen Balykin
  • 731
  • 5
  • 13
  • I tried your solution but it does not work. thank you. – ace Dec 07 '18 at 11:23
  • what does save method return? And could you provide full your code from filter? – Yauhen Balykin Dec 07 '18 at 11:40
  • it returns Mono. I posted another question related to this that shows full source code. https://stackoverflow.com/questions/53651090/how-to-use-subscribe-within-map-context-in-spring-reactor-web-app – ace Dec 07 '18 at 15:07
  • How do you define that User has not saved? – Yauhen Balykin Dec 07 '18 at 19:36
  • Problem occurs in downstream controller endpoints which also modify user and save but actually its not saved. Something could be wrong in other controller code. – ace Dec 09 '18 at 04:59