How to Call a REST API using WebClient in Spring Boot

Поделиться
HTML-код
  • Опубликовано: 11 янв 2025

Комментарии •

  • @zed620
    @zed620 Год назад +3

    Thank you for making the WebClient very simple to understand. Keep up the good work.

    • @Randomcode_0
      @Randomcode_0  Год назад

      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.

  • @alexisraels1437
    @alexisraels1437 Год назад +5

    Great straightforward video. I really appreciate that you got right to the point. Finally gave me the answer I've been looking for.

  • @anishaa3298
    @anishaa3298 Год назад

    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! 😺🐶

  • @ivan-lebedev-e8p
    @ivan-lebedev-e8p 3 месяца назад

    Thank you very much! Really chill video and voice, I enjoy

    • @Randomcode_0
      @Randomcode_0  3 месяца назад +1

      Great to hear! And thank you so much for the compliments, I truly appreciate the kind words!

  • @anshulagarwal6682
    @anshulagarwal6682 11 месяцев назад +2

    How to put headers and body as input to the get or post request

  • @jitendrapatil3276
    @jitendrapatil3276 Год назад

    Simple and easy example to understand !!!
    Are you not closing the WebClient underlying connection ?

    • @Randomcode_0
      @Randomcode_0  Год назад +2

      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.

  • @sp_shaun
    @sp_shaun 2 месяца назад

    when you add .block in chain, its gonna block the thread to wait for response coming

  • @petermuller7246
    @petermuller7246 Год назад +1

    Nice little demo but where is the benefit of going async? With your example there is none.

  • @OffRoadEngineerVlogs
    @OffRoadEngineerVlogs Год назад

    Nice valuable information thanks for sharing it with us ❤

    • @Randomcode_0
      @Randomcode_0  Год назад +1

      I'm thrilled to hear that you found the information valuable! Providing useful information is indeed my goal.

  • @lucasBorba-h3l
    @lucasBorba-h3l 9 месяцев назад

    Someone knows if this dependecy is still working?

  • @ianwanjala8621
    @ianwanjala8621 9 месяцев назад

    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?

    • @Randomcode_0
      @Randomcode_0  9 месяцев назад

      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.

    • @ianwanjala8621
      @ianwanjala8621 9 месяцев назад

      @@Randomcode_0 thanks for the clarification brother.

  • @tristenmartinez3756
    @tristenmartinez3756 2 года назад +1

    Anytime I tried to add the depenency into my project it says it can not be foudn :(

    • @fal8202
      @fal8202 2 года назад +2

      I added it at the point when I was building with Spring initializer, that worked for me

    • @juz4life728
      @juz4life728 Год назад

      My suggestion to this is always "Reload all maven project". It will works.

  • @amanpandey9318
    @amanpandey9318 Год назад

    thanks for saving my time 😉

  • @Devivl
    @Devivl 6 месяцев назад

    Thank you very much, sir.

  • @DalisYn
    @DalisYn 2 года назад

    Can you explain what a mono of a object / class is? I’m still trying to understand

    • @Randomcode_0
      @Randomcode_0  2 года назад +5

      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.

    • @OffRoadEngineerVlogs
      @OffRoadEngineerVlogs Год назад

      @@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?

    • @OffRoadEngineerVlogs
      @OffRoadEngineerVlogs Год назад

      @@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 😊

    • @Randomcode_0
      @Randomcode_0  Год назад +1

      @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.

    • @OffRoadEngineerVlogs
      @OffRoadEngineerVlogs Год назад

      @@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?

  • @dhirajkurupudi3671
    @dhirajkurupudi3671 Год назад

    How to unit test this ?

    • @Randomcode_0
      @Randomcode_0  Год назад

      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.

  • @tispenvinne66
    @tispenvinne66 10 месяцев назад

    thank you ❤❤

  • @kadiraka
    @kadiraka 2 года назад

    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 ?

    • @Randomcode_0
      @Randomcode_0  2 года назад +1

      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.

    • @alexisraels1437
      @alexisraels1437 Год назад +3

      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).

  • @robertosarquis5219
    @robertosarquis5219 Год назад

    Thank you!

  • @andreray1795
    @andreray1795 Год назад

    Great 🙏

  • @skccharan
    @skccharan Месяц назад

    Thanks

  • @quantasync
    @quantasync 2 года назад

    Thanks a lot