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!
Thank you very much, this is very useful
You are welcome! Glad it helped you!
Can you make tutorial on - Polymorphic Deserialization in Jackson (java)
where inner class (inside outer class) is initialized at runTime - polymorphically
Thanks sir, this vidio is very useful for me
Glad it helped you! Thanks for your comment!
subscribed
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?
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!
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() ??
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.
@@raginiyampalliHow so? If the method hadn't yet been set in line 9?
Thx, but how to get value from "Common" string which include in "Name" ?
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
It's work, thx man!@@ifelsestatement7803
How can I tackle it without using Jackson
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?
Where do I put a header for my API token?
It will look like this:
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
@@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();
}
дароу учитель
Ты русский? Акцент похожий
Я из Латвии. Но русский также мой родной язык, так что акцент оттуда, да ;DD