【问题标题】:Retrieve webclient response - Spring Cloud检索 webclient 响应 - Spring Cloud
【发布时间】:2019-06-26 14:20:12
【问题描述】:

我正在使用 spring cloud 从另一个服务请求数据。所以,基本上我请求数据,我想检索该数据并将其分配给另一个对象,该对象将是我要保存的对象。

这是我的代码:

public Mono<Shops> save(Shops shops) {

    Mono<Shops> s = webClientBuilder.build().get()
            .uri("http://mysql-app/api/reservation-passengers/boarding-pass-data/" + shops.getBoardingPassId().toString())
            .exchange()
            .flatMap(response -> {
                Shops myShops = response.bodyToMono(Shops.class).block();
                shops.setAirportDestiny(myShops.getAirportDestiny());
                shops.setCustomerId(myShops.getCustomerId());
                return shopsRepository.save(shops);
            });
    return s;   
}

但是我遇到了一个异常:

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-7

如何从异步方法中获取数据?

【问题讨论】:

    标签: spring-boot spring-cloud spring-webclient


    【解决方案1】:

    我有一个解决方案。我不得不稍微更改我的代码。

    public Mono<Shops> save(Shops shops) {
        Mono<BoardingPassDTO> response = webClientBuilder.build().get()
                .uri("http://mysql-app/api/reservation-passengers/boarding-pass-data/" 
                        + shops.getBoardingPassId().toString())
                .retrieve().bodyToMono(BoardingPassDTO.class);
    
        return response.flatMap(r ->{
                shops.setAirportDestiny(r.getAirportArrivalId());
                shops.setCustomerId(r.getPassengerId());    
                shops.setShopDate(LocalDateTime.now());
                return shopsRepository.save(shops);
            });     
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 2021-01-08
      • 2018-10-17
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2021-09-17
      • 2020-10-30
      • 2020-11-24
      相关资源
      最近更新 更多