as a cat lover I approve this . Also, I modified the code a little by adding controller file for catfacts, and another controller file for dogfacts, and now my API can call both cats and dogs API. thanks to you! 😺🐶
In Spring WebClient, you generally don't need to explicitly close the connection after making a request. The WebClient class in Spring is designed to be non-blocking and uses the Reactor Netty library as its default HTTP client.
I'm not completely sure I understand the question, but you can definitely use multiple libraries that do more or less the same thing in a single project. It might not be the best approach from a long-term development perspective, but if you have a good reason, it should be fine. Using different build tools like Maven or Gradle, you can easily add extra libraries to a project.
A mono is simply an object that can only contain a single element. In this case, we just define that we want our entire return body as a single string if present or empty. The opposite would be a flux which can contain more or the same object. Mono -> contains a single string or empty Flux -> contain multiple strings similar to a List or empty Mono and Flux are both reactive streams. They differ in what they express. A Mono is a stream of 0 to 1 element, whereas a Flux is a stream of 0 to N elements.
@@Randomcode_0i want to get address for customers from third api call with client id, language, and jwt.. how can I achieve? Any help will be appreciated 😊
@SantoshPandeyVlogs YOu will need to use a List or a flux, monos are when a single item is returned. You would use a flux for multiple items. A simple setup could be something like: public Flux getCustomerAddresses(String clientId, String language, String jwt) { return webClient.get() .uri("/addresses") // Assuming the API endpoint returns multiple addresses. .header("Authorization", "Bearer " + jwt) .queryParam("clientId", clientId) .queryParam("language", language) .retrieve() .bodyToFlux(CustomerAddress.class); } But it very much depends on your exact use case.
@@Randomcode_0 thank you so much for such idea. That’s what i need. There are some 5/6 other parameters will be there in headers. And one more question: should I repeat same steps for all apis? As I have 4 endpoints. I need to save and update the address object part into oracle db. I need to map response accordingly right in my dto/pojo in that order only?
The short answer is that I woud not, as there is no domain logic to test. Unit test should be performed on a single unit in your application, often containning some domain logic. I would not unit test exsisting library functioanllities. This is also a very small example and not contian a real application setup allowing for a more proper testing. I would normally have some services handeling domain logic and it would then be the moethods of these services I would unit test.
When I try something like .bodyToMono(Student.class), I get the error : org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType. Do you have any idea what may causes to this ?
You are trying to map your JSON response to an object of type Student, it seems that this mapping is impossible. I would try to just use .bodyToMono(String.class) to see the exact output to check if it matches your Student class. You might also be missing the correct constructor setup, I can't remember if you need an all args constructor or a noargsconstructer with a setter, but you can try both if the JSON looks exactly like your Student class.
You need a no args constructor and setters. The lombok annotations I use are @Data (for getters and setters) @NoArgsConstructor and @ToString for logging (you don't need the last one).
Thank you for making the WebClient very simple to understand. Keep up the good work.
I am glad to hear I was able to present it in a simple and understandable fashion, which is always one of my main goals.
Great straightforward video. I really appreciate that you got right to the point. Finally gave me the answer I've been looking for.
Great to hear!
as a cat lover I approve this . Also, I modified the code a little by adding controller file for catfacts, and another controller file for dogfacts, and now my API can call both cats and dogs API. thanks to you! 😺🐶
Nice work!
Thank you very much! Really chill video and voice, I enjoy
Great to hear! And thank you so much for the compliments, I truly appreciate the kind words!
How to put headers and body as input to the get or post request
Simple and easy example to understand !!!
Are you not closing the WebClient underlying connection ?
In Spring WebClient, you generally don't need to explicitly close the connection after making a request. The WebClient class in Spring is designed to be non-blocking and uses the Reactor Netty library as its default HTTP client.
when you add .block in chain, its gonna block the thread to wait for response coming
Nice little demo but where is the benefit of going async? With your example there is none.
Nice valuable information thanks for sharing it with us ❤
I'm thrilled to hear that you found the information valuable! Providing useful information is indeed my goal.
Someone knows if this dependecy is still working?
is it okay to have webflux and web in the same project or do I have to use a complete reactive project if I want to use webclient?
I'm not completely sure I understand the question, but you can definitely use multiple libraries that do more or less the same thing in a single project. It might not be the best approach from a long-term development perspective, but if you have a good reason, it should be fine. Using different build tools like Maven or Gradle, you can easily add extra libraries to a project.
@@Randomcode_0 thanks for the clarification brother.
Anytime I tried to add the depenency into my project it says it can not be foudn :(
I added it at the point when I was building with Spring initializer, that worked for me
My suggestion to this is always "Reload all maven project". It will works.
thanks for saving my time 😉
Glad it helped!
Thank you very much, sir.
Can you explain what a mono of a object / class is? I’m still trying to understand
A mono is simply an object that can only contain a single element. In this case, we just define that we want our entire return body as a single string if present or empty.
The opposite would be a flux which can contain more or the same object.
Mono -> contains a single string or empty
Flux -> contain multiple strings similar to a List or empty
Mono and Flux are both reactive streams. They differ in what they express. A Mono is a stream of 0 to 1 element, whereas a Flux is a stream of 0 to N elements.
@@Randomcode_0how i can hold response for multiple objects which is coming into response. Should I use List of that response using flux or mono?
@@Randomcode_0i want to get address for customers from third api call with client id, language, and jwt.. how can I achieve? Any help will be appreciated 😊
@SantoshPandeyVlogs YOu will need to use a List or a flux, monos are when a single item is returned. You would use a flux for multiple items. A simple setup could be something like:
public Flux getCustomerAddresses(String clientId, String language, String jwt) {
return webClient.get()
.uri("/addresses") // Assuming the API endpoint returns multiple addresses.
.header("Authorization", "Bearer " + jwt)
.queryParam("clientId", clientId)
.queryParam("language", language)
.retrieve()
.bodyToFlux(CustomerAddress.class);
}
But it very much depends on your exact use case.
@@Randomcode_0 thank you so much for such idea. That’s what i need. There are some 5/6 other parameters will be there in headers. And one more question: should I repeat same steps for all apis? As I have 4 endpoints. I need to save and update the address object part into oracle db. I need to map response accordingly right in my dto/pojo in that order only?
How to unit test this ?
The short answer is that I woud not, as there is no domain logic to test. Unit test should be performed on a single unit in your application, often containning some domain logic. I would not unit test exsisting library functioanllities. This is also a very small example and not contian a real application setup allowing for a more proper testing. I would normally have some services handeling domain logic and it would then be the moethods of these services I would unit test.
thank you ❤❤
When I try something like .bodyToMono(Student.class), I get the error : org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType. Do you have any idea what may causes to this ?
You are trying to map your JSON response to an object of type Student, it seems that this mapping is impossible. I would try to just use .bodyToMono(String.class) to see the exact output to check if it matches your Student class. You might also be missing the correct constructor setup, I can't remember if you need an all args constructor or a noargsconstructer with a setter, but you can try both if the JSON looks exactly like your Student class.
You need a no args constructor and setters. The lombok annotations I use are @Data (for getters and setters) @NoArgsConstructor and @ToString for logging (you don't need the last one).
Thank you!
You bet!
Great 🙏
Thanks
Thanks a lot
You're welcome!