Can I use Spring's WebClient for the synchronous app, and what are the alternatives? - spring-boot

I am creating a Spring Boot MVC application, with Tomcat and JDBC. My app will be an API client, I've learned that I'll need RestTemplate class for API requests. But since Spring 5, RestTemplate is in maintenance mode and will be deprecated in the future.
The alternative is a WebClient class, which is powerful and useful for testing also. But it supports a reactive WebFlux stack.
For example, when I watched this tutorial on how to make tests with WebClient I was very confused:
https://www.youtube.com/watch?v=kGK9Hf8cnBw
I must use these Mono and Flux types?
My first question is, can I use WebClient to make a classic servlet-based app (with Tomcat, and JDBC/JPA like I used to)?
If not, what is an alternative to WebClient and RestTemplate?

WebClient can be used in synchronous style by blocking at the end for the result like this:
Person project = client.get().uri("/project/{id}", i).retrieve()
.bodyToMono(Project.class)
.block();
List<Person> projects = client.get().uri("/projects").retrieve()
.bodyToFlux(Project.class)
.collectList()
.block();
Here we use block() to block the stream and get the data out of it. Note that this shouldn’t be used in a reactive environment.

This is late but someone might benefit from this.. You can use open feign.. Check it out

Related

How to call Async API in Spring Boot using RestTemplate?

I am in a need of calling a api asynchronously in my spring boot application? Is there any methods that we can used in Spring RestTemplate?
RestTemplate may not be a good idea. As per documentation, AsyncRestClient exposes similar methods as RestTemplate, but returns ListenableFuture wrappers as opposed to concrete results. Also, WebClient would be a much better choice over AsyncRestClient which is deprecated. As the linked page shows it is
Deprecated. as of Spring 5.0, in favor of WebClient

calling a rest endpoint in a springboot application

which is better alternative for calling REST endpoint in springboot application, calling REST endpoints using WebClient or calling REST endpoints using RestTemplate ?
Spring’s documentation recommends using WebClient, but that’s only a valid recommendation for reactive apps. If you aren’t writing a reactive app, use OpenFeign instead. Like anything else in software, it fits well for some cases, but might complicate things for others. Choosing WebClient to implement the REST endpoint calls is strongly coupled to making your app reactive
RestTemplate gives many advantages if you are using it from within Springboot application, i.e. in your server side to another part of your own app - sort of like an internal call. Because the RestTemplate "knows" all your entities and beans and so if you need to send over or receive an object which is known within your springboot application RestTemplate can map them automatically which is a very nice advantage. If you sending a request to some third party api and do not pass or receive your known entities RestTemplate is still a valid option but it just becomes just another Http client. Its just simply there as part of Springboot provided tools. But in this case you may use any other client as well.

SpringBoot FeignClient vs WebClient

I want to consume a couple of rest services. Before I have used RestTemplate, but now I want to know What is main diffrences of SpringBoot FeignClient and WebClient?
when they should be used?
To be able to answer “when” one needs to understand the capabilities of each.
Spring WebClient is a non-blocking reactive client to make HTTP requests. Hence if you intend to use Spring Reactive Stream API to stream data asynchronously then this is the way to go. Think event-driven architecture. WebClient is part of the Spring WebFlux library.
[Feign]3 is a declarative REST library that uses annotations based architecture with thread-per-request model. This means that the thread will block until the feign client receives the response. The problem with the blocking code is it must wait until the consuming thread completes, hence think memory and CPU cycles.
So use Spring WebClient when needing non-blocking HTTP requests otherwise Feign due to simple usage model.
(Note: There is no reason as to why one cannot use WebClient for blocking operations but Feign is more mature and it’s annotation based model makes it easier)
The main difference is that WebClient supports Reactive calls.
You can achieve that with 3rd party feign clients like https://github.com/Playtika/feign-reactive but basically for a reactive way you should consider using WebClient with some neat async connector like Jetty. On the other hand, If you want a blocking way with minimal hassle then Feign could be your best choice.
WebClient is a non-blocking reactive.
Feign is blocking.

Does Spring WebFlux automatically close connections when using .retrieve() or .exchange()?

We recently switched from a JAX RS-based WebClient to Spring's reactive WebFlux client. As we had problems with the old client retaining connections in some cases, we were wondering how the WebFlux client treats those. Unfortunately, Spring's documentation for WebFlux doesn't provide details on this, only in the context of WebSockets. So: Does calling .retrieve() or .exchange() automatically close connections? Or do we need to take care of that on our own?

Can I use a standalone WebClient without Webflux

I'm developing a reactive service and preparing to use the WebClient to exchange with HTTP APIs,but the service is not in a reactive web stack so how can I use it(WebClient) without depending on Webflux or is there any alternative reactive HTTP client?
Thanks in advance.
I am not sure if I get your question correct: You want to use an inherently reactive class without the reactive lib in which it is contained?
As you can see via the link you put in your question, the WebClient is part of spring-webflux and depends e.g. on reactor.core.publisher.Mono which resides in compile("io.projectreactor:reactor-core"). I can not imagine any scenario in which this WebClient would work or make any sense as you've asked "without depending on Webflux".
Other reactive HTTP clients are:
RxHttpClient
Java HTTP Client introduced in Java 11
May be you could elaborate a little bit more on your needs, why you won't rely on WebFlux or why you want to use a reactive client in a non-reactive stack.

Resources