How to send a GET request and fetch a response as a JAVA OBJECT?

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • In this GET request tutorial, you will learn about what is a GET request, how to send it in Java, and how to convert the response from this request to a plain Java object using an ObjectMapper.
    Code: github.com/if-...
    RestCountries API: restcountries....
    Why I have used a StringBuilder and not a String in this tutorial when I have been fetching a response from a GET request: • Why you should NOT con...
    Thanks for watching!

ความคิดเห็น • 22

  • @anirudh514
    @anirudh514 5 หลายเดือนก่อน

    Thank you very much, this is very useful

  • @shubhamagarwal9182
    @shubhamagarwal9182 28 วันที่ผ่านมา

    Can you make tutorial on - Polymorphic Deserialization in Jackson (java)
    where inner class (inside outer class) is initialized at runTime - polymorphically

  • @muhammadyusup4239
    @muhammadyusup4239 9 หลายเดือนก่อน

    Thanks sir, this vidio is very useful for me

    • @ifelsestatement7803
      @ifelsestatement7803  9 หลายเดือนก่อน

      Glad it helped you! Thanks for your comment!

  • @shubhamagarwal9182
    @shubhamagarwal9182 28 วันที่ผ่านมา

    subscribed

  • @neallyrice
    @neallyrice 3 หลายเดือนก่อน

    Is it possible to get a complete mapping from JSON to Java object without having to implement it beforehand (like you did with Country class)? Like a literal translation from the complete JSON string?

    • @ifelsestatement7803
      @ifelsestatement7803  3 หลายเดือนก่อน

      Yes, you can convert JSON to the Java hashmap where the key is the String and the value is an Object class. Does it answer your question? Thanks!

  • @ece03abhisheksrivastava64
    @ece03abhisheksrivastava64 6 หลายเดือนก่อน

    Thank you sir for explanation but just a question at which line the api request is fired ( or api request is callled)
    like at
    connnection.setRequestMethod();
    or connection.getResponseCode() ??

    • @raginiyampalli
      @raginiyampalli 6 หลายเดือนก่อน

      The actual API call is made when the connection is established and the request method is set. In this video, the API call occurs implicitly when the urlObj object's openConnection() method is called to establish a connection to the test url.

    • @neallyrice
      @neallyrice 3 หลายเดือนก่อน

      ​@@raginiyampalliHow so? If the method hadn't yet been set in line 9?

  • @vladimirputin1148
    @vladimirputin1148 10 หลายเดือนก่อน

    Thx, but how to get value from "Common" string which include in "Name" ?

    • @ifelsestatement7803
      @ifelsestatement7803  10 หลายเดือนก่อน

      Good question! One of the ways can be defining a generic hashmap of name "name" in a Country class and to make a key to be of type String and Value of type Object (so that it would not break for some other types). After that you can just do name.get("common"). This should work

    • @vladimirputin1148
      @vladimirputin1148 10 หลายเดือนก่อน

      It's work, thx man!@@ifelsestatement7803

  • @richardnnaji9686
    @richardnnaji9686 4 หลายเดือนก่อน

    How can I tackle it without using Jackson

    • @ifelsestatement7803
      @ifelsestatement7803  4 หลายเดือนก่อน

      There are some other libraries that can offer alternative solutions but in general, Jackson is the most popular option. Why do you need to avoid using it?

  • @tyjantefinn4791
    @tyjantefinn4791 8 หลายเดือนก่อน

    Where do I put a header for my API token?

    • @ifelsestatement7803
      @ifelsestatement7803  8 หลายเดือนก่อน +1

      It will look like this:
      connection.setRequestProperty("Authorization", "Bearer " + accessToken);

    • @tyjantefinn4791
      @tyjantefinn4791 8 หลายเดือนก่อน

      @@ifelsestatement7803
      I actually solved it using this!
      try {
      HttpRequest httpRequest = HttpRequest.newBuilder()
      .uri(new URI(myAPIURL))
      .header("Authorization", apiKey)
      .build();
      } catch (URISyntaxException use) {
      use.printStackTrace();
      } catch (Exception e) {
      e.printStackTrace();
      }

  • @hollowmoon_
    @hollowmoon_ 2 หลายเดือนก่อน

    дароу учитель

  • @ship-yw7xk
    @ship-yw7xk 4 หลายเดือนก่อน

    Ты русский? Акцент похожий

    • @ifelsestatement7803
      @ifelsestatement7803  4 หลายเดือนก่อน +1

      Я из Латвии. Но русский также мой родной язык, так что акцент оттуда, да ;DD