~ Реактивный и неблокирующий
~ Работает на event-loop, а не на модели один поток на запрос
~ Отлично подходит для микросервисной архитектуры
❌ RestTemplate (blocking)
@Service
public class ItemService {
private final RestTemplate restTemplate;
public ItemService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public Item getItem(Long id) {
return restTemplate.getForObject(
"https://api.example.com/items/{id}",
Item.class,
id
);
}
}
✅ WebClient (non-blocking)
@Service
public class ItemService {
private final WebClient webClient;
public ItemService(WebClient webClient) {
this.webClient = webClient;
}
public Mono<Item> getItem(Long id) {
return webClient.get()
.uri("/items/{id}", id)
.retrieve()
.bodyToMono(Item.class);
}
}
👉 Java Portal
