WebFlux系列(十)WebClient 异常处理

#Java#Spring#WebFlux#Reactor#WebClient#异常处理#

WebClient 异常处理

视频讲解: https://www.bilibili.com/video/av83495354/

WebFlux系列(十)WebClient 异常处理_第1张图片

 

代码:

@GetMapping(value = "employees", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux findAll() {
  return WebClient.create("localhost:8080/employees")
      .get()
      .retrieve()
      .onStatus(HttpStatus::is5xxServerError, clientResponse -> {
        return Mono.error(new Cum5xxException(clientResponse.statusCode().value() + " error code"));
      })
      .onStatus(HttpStatus::is4xxClientError, clientResponse -> {
        return Mono.error(new Cum4xxException(clientResponse.statusCode().value() + " error code"));
      })
      .onStatus(HttpStatus::isError, clientResponse -> {
        return Mono.error(new Cum4xxException(clientResponse.statusCode().value() + " error code"));
      })
      .bodyToFlux(Employee.class);
}

公众号,坚持每天3分钟视频学习

WebFlux系列(十)WebClient 异常处理_第2张图片

你可能感兴趣的:(WebFlux系列(十)WebClient 异常处理)