Igor Fedorov
Igor Fedorov

Reputation: 349

Spring WebFlux and WebClient change response on error

I have some controller method like

@PostMapping("/*")
fun proxy(@RequestBody body: String): Mono<ByteArray> {
    return roundRobinBean.getNext()
        .post()
        .uri("/api")
        .body(BodyInserters.fromObject(body))
        .retrieve()
        .bodyToMono<ByteArray>()
        .doOnSuccess{
            threadPool.submit(PutToCacheJob(body, it, cacheBean))
        }
        .doOnError{
            logger.error(it.message, it)
        }
}

roundRobinBean return WebClient for some host. If i get connection timeout exception or get 500 response i need call another host or return data from cache. Have mono some handler for changing inner data?

Upvotes: 0

Views: 1195

Answers (1)

Martin Tarj&#225;nyi
Martin Tarj&#225;nyi

Reputation: 9937

You can use onErrorResume operator which lets you define a fallback in case of errors.

Upvotes: 1

Related Questions