检索网络客户端响应 - Spring 云
Retrieve webclient response - Spring Cloud
我正在使用 spring 云从其他服务请求数据。所以,基本上我请求数据,我想检索该数据并将其分配给另一个对象,该对象将是我要保存的对象。
这是我的代码:
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
如何从异步方法获取数据?
我找到了解决办法。我不得不稍微更改一下代码。
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);
});
}
我正在使用 spring 云从其他服务请求数据。所以,基本上我请求数据,我想检索该数据并将其分配给另一个对象,该对象将是我要保存的对象。
这是我的代码:
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
如何从异步方法获取数据?
我找到了解决办法。我不得不稍微更改一下代码。
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);
});
}