Send HTTP GET Request using Java 11 HttpClient and parse JSON response into objects

Sdílet
Vložit
  • čas přidán 4. 09. 2024
  • In this tutorial, you will learn how to make an HTTP request in Java using the HttpClient class from Java 11.
    When you have completed this tutorial you should understand:
    ✅ What is the HttpClient class and why would you need it
    ✅ Use Java HttpClient to send an HTTP GET Request
    ✅ Call a REST Client in Java
    ✅ Use Jackson Databind to parse JSON into Java Objects
    ✅ Use a reference method to print out each Java Object
    🔗Resources & Links mentioned in this video:
    Source Code: github.com/dan...
    👋🏻Connect with me:
    Website: www.danvega.dev
    Twitter: / therealdanvega
    Github: github.com/dan...
    Instagram: / thedanvega
    LinkedIn: / danvega
    Coffee & Code: www.danvega/de...
    SUBSCRIBE TO MY CHANNEL: bit.ly/2re4GH0 ❤️
    ------------------------------
    Timeless by Scandinavianz / scandinavianz
    Creative Commons - Attribution 3.0 Unported - CC BY 3.0
    Free Download / Stream: bit.ly/_timeless
    Music promoted by Audio Library • Timeless - Scandinavia...
    ------------------------------

Komentáře • 72

  • @nakuldighe8638
    @nakuldighe8638 Před 4 lety +10

    I almost thought it is impossible to learn http requests in java from the internet until i saw this video, thanx man. Great video by the way

    • @DanVega
      @DanVega  Před 4 lety +2

      Wow, that is so awesome of you to say. Thank you for watching.

  • @raymondkosos
    @raymondkosos Před 3 lety +5

    Nice! Lot's of API's out there with data returned in JSON format - this tutorial is exactly what I needed to get started! Learning about the .forEach was also a bit of a bonus. I think all I need to do is find this done asynchronously and I'm all set ...

  • @superiorautism
    @superiorautism Před rokem +2

    Dan, thank you so much for this tutorial!
    I'm completely new to Java, and I feel that there are very few resources for learning these materials (compared to something like NodeJS or Python). It's greatly appreciated!

  • @rogeriosartori5644
    @rogeriosartori5644 Před 4 lety +6

    Thanks man. Great tutorial!
    Simple and easy to understand. The best tutorial of http request in the internet.

  • @pablohernandez4305
    @pablohernandez4305 Před 3 lety +2

    You saved my life, thank you for this video i had about two days with this topic and it was so easy.

  •  Před 4 lety

    I used to hate Java, but you just did make I love it.

  • @QwinTube
    @QwinTube Před 3 lety +2

    Its great! Thanks very much!!! Please do more....

  • @shwetaadurkar562
    @shwetaadurkar562 Před 2 lety +1

    Thank you!!..you made it simple and easy to understand.

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 Před 6 měsíci

    Great tutorial! Thank you so much! 💛

  • @_kishorroy
    @_kishorroy Před 2 lety

    This video helped me a lot in my learning phase. Thanks.

  • @amirbeheshtian8426
    @amirbeheshtian8426 Před 2 lety

    Great toturial man thanks.
    Could you please make a toturial for Post method ?
    specially for this scenario :
    - Post a Json object ( which include user and pass)
    - get the value for token in response body.

  • @batesinheritance
    @batesinheritance Před 2 lety

    Thank you very much. You helped me understand HttpClient Get() method.

  • @kriszteblade
    @kriszteblade Před rokem

    Perfect tutorial.

  • @edwinkipruto7125
    @edwinkipruto7125 Před 3 lety

    that was very easy to understand, would you kindly make a video in consuming rest services using authentications , and web tokens

  • @andrii5866
    @andrii5866 Před 4 lety +2

    Thanks man. Great tutorial!
    Have you ever thought about a new video about Spring boot + GraphQl or Spring boot + GraphQl + security?
    I think it's a great topic for 2020.

    • @DanVega
      @DanVega  Před 4 lety +3

      Thank you. I have really been enjoying using GraphQL on my personal site this past year. Yes, Spring Boot + GraphQL is on my list.

  • @samehyoussef4988
    @samehyoussef4988 Před 3 lety +1

    how to add password and username for authentication?

  • @vanshrana9508
    @vanshrana9508 Před 2 lety +1

    It was very helpful, thanks a lot :)

  • @Hector21211
    @Hector21211 Před 2 lety +1

    excelente tutorial saludos desde argentina

  • @srinivasmurthy3386
    @srinivasmurthy3386 Před 3 lety +1

    Nice!. Thanks for the g8 video.

  • @nurba_nurba
    @nurba_nurba Před 4 lety +9

    video starts from 6:03. thank me later

  • @abigailedwards1238
    @abigailedwards1238 Před 9 měsíci

    Thanky ou this is exactly what i needed

    • @DanVega
      @DanVega  Před 9 měsíci

      Glad I could help out, let me know if you have any other questions about clients in Java

  • @michaelnewman2993
    @michaelnewman2993 Před 3 lety +1

    This may be a dumb question, but what if you only need to access a few specific fields of your Json object? For instance if I was using a geocoding api and only needed to return the latitude and longitude. Thanks for the video extremely helpful!

    • @conradpeterson1387
      @conradpeterson1387 Před 3 lety

      You've probably already figured this out, but you can just add the annotation "@JsonIgnoreProperties(ignoreUnknown=true)" to the object class and then you only have to include variables in the class for the specific fields you want.

  • @pajeetsingh
    @pajeetsingh Před rokem +1

    Do you think adding application related functionality directly into a programming language somehow defeat incentive to learn the language itself?

    • @DanVega
      @DanVega  Před rokem

      Not at all. Having to write low level code to interact with Http doesn't sound like fun to me. Program languages give us the ability to perform common tasks and I think this is a great abstraction here.

  • @laponiec
    @laponiec Před rokem

    Is it possible to set multiple headers in our GET/POST method? In my program I need to set the authorization header (basicAuth), and I wonder if I can also set the content type.

  • @user-yf8zd1tw7l
    @user-yf8zd1tw7l Před 3 lety +1

    very cool, thanks!

  • @grrlgd3835
    @grrlgd3835 Před 3 lety

    Tks Dan , very useful. how about to get an image ?

  • @praveens2272
    @praveens2272 Před rokem

    Is it possible to map the response with body handler itself ?

  • @stanislavivanov9817
    @stanislavivanov9817 Před 4 lety

    Thank you for this tutorial.

    • @DanVega
      @DanVega  Před 4 lety

      Thanks for watching 😊

  • @haydn.murray
    @haydn.murray Před 4 lety

    Hey Dan - great tutorial!! - What would you recommend when your JSON is a map....ie `{"someData": [{a: 1, b: 2}]}` and all I want is the "someData" value.
    Map posts = mapper
    .readValue(response.body(), new TypeReference() {});
    posts.get("someData") //

  • @alexandroskourtis5268
    @alexandroskourtis5268 Před 3 lety +1

    awesome tutorial :)

  • @alpanajyoti7774
    @alpanajyoti7774 Před 3 lety

    How one can use interceptor to maintain session cookies for okHttpclient with core Java?

  • @zorfrak
    @zorfrak Před 2 lety

    I'm able to retrieve the data from the API, but but I get IllegalStateException("Cipher not initialized"). Can I prevent this?

  • @wiliamferraciolli5380
    @wiliamferraciolli5380 Před 3 lety

    Can you show us how to unit test it please? with mockito

  • @MegaMoses91
    @MegaMoses91 Před 3 lety

    Great Tutorial!

  • @7thwik
    @7thwik Před 2 lety

    Can someone explain on how to unit test these requests, responses using mockito ?

  • @hariprasadbs966
    @hariprasadbs966 Před 3 lety

    I need this option in single java class file without any server like tomcat,glassfish or others
    When I try in single java class, I says error: No classdef error HTTPurirequest not found
    How to fix this

  • @TTYounga
    @TTYounga Před 3 lety

    Thank you so much!

  • @jafd7806
    @jafd7806 Před 4 lety

    Good content Bro 👌

  • @nikhilbansal196
    @nikhilbansal196 Před 4 lety +1

    Thanks for the information. I have a question : how can we send a get request with parameters using HttpClient in java 11?

    • @lieutenantflogy6183
      @lieutenantflogy6183 Před 4 lety

      I am curious about this as well. I've been looking for an answer and have yet to find one.

  • @devinbaack3578
    @devinbaack3578 Před 2 lety

    How would this work for nested Json?

  • @iVarLitap
    @iVarLitap Před 4 lety

    If i want to consume this web service then how can get object reference of Post class

  • @marlegagaming1274
    @marlegagaming1274 Před 3 lety

    I'm getting this error, could you please help me out
    readValue(JsonParser, Class) in the type ObjectMapper is not applicable for the arguments (Object, new TypeReference(){})

    • @marlegagaming1274
      @marlegagaming1274 Před 3 lety

      Never mind, fixed it, programming in a nut shell🤣
      One second : no idea what's going on
      After making minor changes : I'm the best 🤣😉
      Thanks for the great video.

  • @cypherliquid
    @cypherliquid Před rokem

    what if we want to send a jwt token.

  • @ruhnet
    @ruhnet Před 4 lety

    Fantastic video! Thanks so much for taking the time to share it. I'm not fluent in Java but your video was still understandable and useful! One thing though, for future reference, the proper pronunciation of JSON is "jaysun", rather than "jay-sawn". 😀

    • @arkanglegeibriel
      @arkanglegeibriel Před 3 lety +2

      "There's a lot of argument about how you pronounce that. I strictly don't care. I think the proper pronunciation is probably..." and then the guy uses a strange french-sounding pronunciation.
      So who said this? Well that'd be Douglas Crockford, the guy who invented the format. He says it like you do "jaysun". And I know this comment is old, but it's one of the top of this video so I'ma just... correct that number and that statement and move on, have a nice day ^_^

  • @alexandredemeloferreira5915

    is there a way to send a body on request in a get method?

    • @Rajesh-qk1ne
      @Rajesh-qk1ne Před rokem

      Use Postman tool. It has an inbuilt option to do that

  • @20M_Target_1.0
    @20M_Target_1.0 Před rokem

    How to login to Salesforce using Java 11 httpclient
    How get auth token and instanceUrl??

  • @nidhishah9701
    @nidhishah9701 Před 2 lety

    how can we achieve this with localhost?

  • @FellTheSky
    @FellTheSky Před 3 lety

    I need to send first an authentication request to get SESSION ID cookie and then make another request to get the data. Do you have a video about this, or know where i can read it? Im having a hard time finding material

    • @alpanajyoti7774
      @alpanajyoti7774 Před 3 lety

      did you get solution of above query? I have same problem, let me know , If you got solution.

    • @FellTheSky
      @FellTheSky Před 3 lety

      @@alpanajyoti7774 String cookie = response.headers().firstValue("Set-Cookie").toString();
      String cCookie = cookie.replaceAll(".+\\[", "");
      String cleanCookie = cCookie.split(";")[0];
      System.out.println(cleanCookie)
      Then on the second request: .setHeader("Cookie",cleanCookie)

  • @Pritam252
    @Pritam252 Před 3 lety

    Why my one says GET() is undefined?

    • @DanVega
      @DanVega  Před 3 lety

      Do you have the correct imports?
      docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.Builder.html

  • @ArunKumar-oi1yu
    @ArunKumar-oi1yu Před rokem

    I am getting exception when i invoke this "PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)"

  • @ritiksoni8487
    @ritiksoni8487 Před 5 měsíci +1

    what was that intro ?

    • @DanVega
      @DanVega  Před 5 měsíci

      It was an old intro I had made. I don't use it anymore though

  • @japanboy31415
    @japanboy31415 Před rokem

    @7:30

  • @30sana
    @30sana Před 3 lety

    Honestly after all that ima just consider learning python to use requests easier wtf xD