[HowTo | SpringBoot] Call External APIs in SpringBoot using RestTemplate. E.g. using CovidAPIs
Вставка
- Опубліковано 17 вер 2024
- In this video, I will be showing how to call the external API to get the data in Springboot using RestTemplate.
your task: To improve upon this code, you should try to create a POJO that will parse the JSON , and give only the country and no of covid cases in total.
Github link: github.com/atq...
If it's helpful, then buy me a coffee:
Paypal: paypal.me/atqu...
UPI: Atquil@upi
Thanks brother very helpful
Happy to help
very helpful
very helpful
Glad to hear that
One question, from the response body we get the json value right but what if from the result i only want certain value for example just the name of country , then what should we do?, i did try to serialise the json into pojo but it didn't work
Sir you don't have control over external endpoint , about what it will send. You can just map the json object that you require and leave the rest. This will be the easiest and fastest way.
@@atquil1032 agreed but i am not talking about modifying the endpoint itself , what I mean is for example...imagine this is the result i get from the response entity 200:{ country :" usa " , state:" Texas" ... And so on } , now i want is after I get the response body only what i want to print or get the country, i don't want the response body to include state or anything else , just print out country :usa , country : India and so on
@@prethammuthappabs2241 You need to create an Object of the response that want to use, then as the values are in list you will do something like
ResponseEntity response = restTemplate.exchange(url,
HttpMethod.Get,
new HttpEntity(headers),
new ParameterizedTypeReferencecovidData.country)
.toList();
Something like this. I hope this helps
@@atquil1032 yess this is what I meant , i did try something similar to this but i was getting null response body,but I'll try your method ones , thank you for your time and response :)
Thanks bro
Here is the GitHub Repo for this project: github.com/atquil/HowToSpringBoot/pull/new/Access_To_External_Endpoint
bro can you add pom.xml file in your GitHub repo i wanted to see which dependency's you are using in this project please..
@@aakashtiwari3868 Though i am using gradle for dependency management ( github.com/atquil/HowToSpringBoot/blob/0001-Access_To_External_API_e.g_RapidAPI_covid_data/build.gradle ) the similar things will look like this in pom
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.10
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
-- One more thing, if you want to use maven (pom.xml) file for dependency than at 3:15 , while creating the project please choose MAVEN from Project Structure so that it will generate pom.xml. One more think , please use version 2.7.* some version of spring boot
why did you create an extra configureation package for resttemplatebuilder? it can be created anywhere within the application when it's needed. am i right?
Yes Sir, you are a absolutely right.
Its just the best practices to put all the configuration that you do for the application at the same place.
Like we do for - business logic in service packages, endpoint in controller package etc.
@@atquil1032 thank you sir, for your explanation😁
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target...I am getting this error...please help
Hi @Mom,
I believe it's due to wrong installation of @JDK. If possible can you please reinstall it. Probably Java11 JDK.
I am getting the same error, any tips on how this can be resolved.
Can you plz tell what is the version of jdk you are using.. and which ide you are using
i have followed the video and it works using http client
and doesn't print any logs anywhere in console..
however i need to make it run post exporting as a maven jar with cmd
and get back a production grade response.
I am sorry, but can you please clarify a little bit more.
About logs are you using @sl4j...
About production grade response, generally, we return status code and the response json object, but do you need anything more..
Hi @@atquil1032
thanks for replying back..
i just wish to get a response back from a production level api.
I am able to get the response only after adding the http client using in vs code(as the same extension intellij is discontinued in community edition)
but post exporting the maven jar,
i want to get the response back after running via the cmd.
How can I use this program to accessing endpoints with basic authentication. how can i pass username and password??
Hi,
Adding the basic auth details is fairly simple ,
String username = "your_username";
String password = "your_password";
// Create the basic authentication token
String credentials = username + ":" + password;
String base64Credentials = Base64.getEncoder().encodeToString(credentials.getBytes()); // For basic encoding of the credentials
then , in the service file where you are creating the request, just add one more header
headers.set("Authorization", "Basic " + base64Credentials);
I hope this helps.
bro, one doubt suppose i have multiple api key and host and i want to change it dynamically is it possible?
Yes buddy, do you have any condition, based on which you are going to decide when to choose which key and host.
What i would have done is called a method --
1. Create A EnvObject (private String host; private String key;)
1. Suppose based on environment you want to call the API
EnvObject getHostAndKey(String environment){
EnvObject envObject = getEnvAndHostBasedOnEnv(environment) // where ever you have saved it
return envObject
}
now use this method getHostAndKey(environment), where environment is your dynamic value,..
try{
EnvObject envObject = getHostAndKey(prod);
String key = envObject.key();
String host = envObject.host();
and use these to substitute in the API Call
}
catch ...
Angular puri video bana bro hindi
Hey Sorry @magiccraftideas. I have not worked on Angular. But here is one good course : ua-cam.com/video/PSRhmtiRNm4/v-deo.html&ab_channel=CodeStepByStep
Hindi bana video
I got error near log
Can you please tell where or put the error logs here in comment.
@@atquil1032 In COVID Service i am not able to import for this line [(log.info("Output.....] Especially for log
Did you add Lombok for dependency .
And @Sl4j just before the class starts...
As an annotations
e.g
@SL4j
class abc
hello@@atquil1032getting this error "log cannot be resolved" please help....
i have added @Slf4j annotation but still i am getting the error