0

我正在使用 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

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

4

1 回答 1

0

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

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);
        });     
}
于 2019-06-28T14:01:18.370 回答