I am looking for a HTTP request library which is RxJS based. Just a tool Angular 4.3+ HTTPClient.
import { HttpClient } from '#angular/common/http';
I want to use a RxJS based http client, not in an Angular Project. It's better to have Interceptors feature and based on fetch API(not xhr).
I have look at rx-http-request, but it seems not too much people use it.
Related
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
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.
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.
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.
we have a Spring project that is about to go into production. Currently, the project is using Apache Http Client. There is a thought of using RestTemplate as HttpClient.
I am digging around to see any notable advantage of using RestTemplate over Apache's. Also, it would be interesting to know what HTTP transport does RestTemplate in its implementation. Apache Http Client has been used by several groups for many years and has a good reputation.
would we be risking moving to RestTemplate?
Further, this blog points that RestTemplate needs to be configured for production, although the configuration is minimal.
Thanks
RestTemplate and HttpClient don't operate at the same abstraction level.
HttpClient is a general-purpose library to communicate using HTTP, whereas RestTemplate is a higher-level abstraction, dealing with JSON/XML transformation of entities, etc.
RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.
So, if the goal is to communicate with a Restful API, and you still want to use HttpClient, you can use RestTemplate over HttpClient.
Note that what I just said is exactly what the blog you linked to explains:
So, the solution is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory, which is a ClientHttpRequestFactory delegating the creation of the requests to an HttpClient.