[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

КОМЕНТАРІ • 43

  • @user-qb1gk2yd5j
    @user-qb1gk2yd5j 6 місяців тому +1

    Thanks brother very helpful

  • @DeepakGaud-h1o
    @DeepakGaud-h1o Рік тому +1

    very helpful

  • @Ajaypatel-me7zn
    @Ajaypatel-me7zn 6 місяців тому

    very helpful

  • @prethammuthappabs2241
    @prethammuthappabs2241 2 місяці тому +1

    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

    • @atquil1032
      @atquil1032  2 місяці тому

      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.

    • @prethammuthappabs2241
      @prethammuthappabs2241 2 місяці тому +1

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

    • @atquil1032
      @atquil1032  2 місяці тому +1

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

    • @prethammuthappabs2241
      @prethammuthappabs2241 2 місяці тому +1

      @@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 :)

  • @user12345i-aztty
    @user12345i-aztty 8 місяців тому

    Thanks bro

  • @atquil1032
    @atquil1032  Рік тому +1

    Here is the GitHub Repo for this project: github.com/atquil/HowToSpringBoot/pull/new/Access_To_External_Endpoint

    • @aakashtiwari3868
      @aakashtiwari3868 Рік тому

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

    • @atquil1032
      @atquil1032  Рік тому

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

  • @HappySmite
    @HappySmite Рік тому +1

    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?

    • @atquil1032
      @atquil1032  Рік тому +2

      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.

    • @HappySmite
      @HappySmite Рік тому

      @@atquil1032 thank you sir, for your explanation😁

  • @Mom11116
    @Mom11116 Рік тому +1

    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

    • @atquil1032
      @atquil1032  Рік тому

      Hi @Mom,
      I believe it's due to wrong installation of @JDK. If possible can you please reinstall it. Probably Java11 JDK.

    • @shreyasitiwari
      @shreyasitiwari Рік тому

      I am getting the same error, any tips on how this can be resolved.

    • @atquil1032
      @atquil1032  Рік тому

      Can you plz tell what is the version of jdk you are using.. and which ide you are using

  • @siddhantnaik7083
    @siddhantnaik7083 7 місяців тому

    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.

    • @atquil1032
      @atquil1032  7 місяців тому

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

    • @siddhantnaik7083
      @siddhantnaik7083 7 місяців тому

      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.

  • @rashmi367
    @rashmi367 7 місяців тому

    How can I use this program to accessing endpoints with basic authentication. how can i pass username and password??

    • @atquil1032
      @atquil1032  7 місяців тому

      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.

  • @simpleman7461
    @simpleman7461 Рік тому +1

    bro, one doubt suppose i have multiple api key and host and i want to change it dynamically is it possible?

    • @atquil1032
      @atquil1032  Рік тому

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

  • @magiccraftideas
    @magiccraftideas Рік тому

    Angular puri video bana bro hindi

    • @atquil1032
      @atquil1032  Рік тому

      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

  • @magiccraftideas
    @magiccraftideas Рік тому +1

    Hindi bana video

  • @mrspoojita6664
    @mrspoojita6664 Рік тому

    I got error near log

    • @atquil1032
      @atquil1032  Рік тому

      Can you please tell where or put the error logs here in comment.

    • @mrspoojita6664
      @mrspoojita6664 Рік тому

      ​@@atquil1032 In COVID Service i am not able to import for this line [(log.info("Output.....] Especially for log

    • @atquil1032
      @atquil1032  Рік тому

      Did you add Lombok for dependency .
      And @Sl4j just before the class starts...
      As an annotations
      e.g
      @SL4j
      class abc

    • @aakashtiwari3868
      @aakashtiwari3868 Рік тому

      hello@@atquil1032getting this error "log cannot be resolved" please help....

    • @aakashtiwari3868
      @aakashtiwari3868 Рік тому

      i have added @Slf4j annotation but still i am getting the error