springspring-bootspring-webfluxspring-cloud-gateway

Cannot resolve method 'doOnSuccessOrError' in 'Mono'


I have this Spring Gateway code which I want to migrate to latest Spring Cloud 3:

@Component
public class CorrelationIdApiFilter implements WebFilter {
  
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    if (......) {
      ServerWebExchange obj = ......

      return chain.filter(obj).doOnSuccessOrError((resp, ex) -> MDC.clear());
    }
    return chain.filter(exchange);
  }
}

I get error: Cannot resolve method 'doOnSuccessOrError' in 'Mono'

I tried to edit the code this way:

@Component
public class CorrelationIdApiFilter implements WebFilter {
  
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    if (......) {
      ServerWebExchange obj = ......

      return chain.filter(obj).doOnSuccess((resp) -> MDC.clear()).doOnError(ex);
    }
    return chain.filter(exchange);
  }
}

But I get Cannot resolve symbol 'ex'. Do you know what should be the proper way to migrate the code?


Solution

  • doOnSuccessOrError is deprecated and removed in 3.4.0. According to documentation

    prefer using doOnNext(Consumer), doOnError(Consumer), doOnTerminate(Runnable) or doOnSuccess(Consumer)

    In your case

    return chain.filter(obj)
        .doOnSuccess(resp -> MDC.clear())
        .doOnError(ex -> MDC.clear());