How to Consume REST APIs in Spring Boot - 2 Methods (RestTemplate and Feign Client)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you are building an application there's possibility that you might need to use a list of data from somewhere for instance a list of countries or a list of diseases or maybe they copied 19 data say these are data that you don't have in your application and you need to actually get them from from wherever they are and using your application and we call this consuming a REST API let me give you an example for instance you may have something like this they say look how I look how data I have on my system the list of items our service okay so we have this list of items here running here but these are local API I build so I can build an application that consumes gets this list and displayed on a table or using let's say for instance we have lists of countries list of countries API we have rest countries and for instance if you want to get a list of countries you can just go down and go to just go down a little bit so you can have this end point here and if you copy and then paste it in your browser you have a list of countries that comes for you a suggestion so how do you get this list of items and then use it right in your application and that is what we are going to be talking about today so let me just show you the steps I'm gonna I'm gonna be doing it using spring boots or spring framework and it's gonna be really easy so I recommend you subscribe to my channel and also feel free to let me know if you have any challenges by our living me a comment in the comment box below so there are two ways to do it one is a simple one in front the two ways as simple as not making a mistake the first the first one is to use the rest template and the second one is to use ribbon and faint lines so let's do both of them let's start with the simple one and then do the other one so I'm going to go to file new let me just create a simple application that I'm going to use to consume these REST API so I'm going to create a new project sprague initializer suppose you should select so that will is set up the initial project for you so now the dependencies you are going to be adding is training they so let's first give the group ID conducts Jenny's the name of the other practice consume API does name of our projects and so I'm going to just call it on June REST API right so if we are going to be using rest templates that we can only add one dependency and that dependency is they are the spring web dependency but if we also want to consume a recipe I using ribbon and thing we need to add ribbon and Fame dependency so let's add all this tree dependencies and then you can see how each of them works so anyone that is easy for you you can actually use so I won't go to click on web by this place and click on spring break and I'll also add Fame EDI so it tells me open paint that is fine and finally I'm going to ask for rhythm okay a ribbon okay prophase hopefully I'm not forgetting anything I'm going to click on this and I'm going to just just leave it in place is okay so I'm going to click on this window yes new window okay so first I'm going to make this application consume a rest api using the using the rest template so to do that there are the two steps to follow the prospect to follow is to simply go to RC main and you'll need to undertake so let's wait for a second all right so the first thing you want to do here is to go to the main application class and I'm not eighty tweet and pray it's a being so I've got to create a restaurant light beam because we are going to be using a restaurant plate to consume the rest api so the credit being simply say simply say arts RB I'm sorry a Dean and then credit class is a bare metal tower towns arrest and plate so I'm gonna say public rest and plate I'll get dressed and plate sorry okay written new registered plates okay so I have this bean try can I try to use this being in a controller file to get their list of items so first I'm gonna import this class so this is my application now I wanted to run on maybe pods 99,000 some business a server the port is equal to nine thousand another thing that we also like to do I like to do it it's not compulsory but it's necessary to give your application in name so to do that is simply say string dot application dot name I'm going to call it consume API so lazy okay so they see the convention so it simply I'll give this name using dash in between the two words alright so let's now save everything and then let's create a controller file that actually does the rest API calls I'm going to right click on this place so my system is a bit slow now alright so I'm gonna say new Java class I'm gonna call it a rest controller I'm gonna say rice let's call it let's call it API controller right then call it a PRS control either way you want so you always have to undertake if you have a controller class the tops of consume api's you need to rotate this class meet at rest control elevation so I'm going to say us rice not rest buying rice on troller annotation right so now let's now write the method that is going to consume this list of countries that we mentioned so I'm going to say first I'm going to open y up the restaurant plate into this class because we're going to be using the restaurant plate to make this call so I'm going to see quiet all four wires let us say private go to wear something simply create a private member virbull of that pipe and then annotate with address template so I'm going to say private restaurant plate the rest and plate okay perfect so when this code runs is going to look for the bean that corresponds to this and using so I'm gonna say let's first get the list of countries in our application so I'm gonna say it's guest nothing to I'm gonna come back to this for now just give me a second some public list of object get countries so this is going to make an external call to this API endpoint I showed you that is some weight on the line online and get the list of countries so I'm going to first I'm going to import this class here least the Java dot util okay so now to make your Resco you'll need to use something called the the they get for object method of the restaurant link so let's see how it goes so I'm going to say first I need to specify the endpoints so let's see the URL in points I'm gonna say private string don't make a static string URL is a call to sew a little bit the URL where the data is located that is what we need so these are URL where they did I took it at so I'm going to simply copy it and uses that control V alright okay so this is the URL of the data so I'm going to say I'm gonna say list of objects of service say first object object array countries is equal to now we're gonna use the rest templates get four objects so this the syntax make no mistake about it so another theory simply dog gets four object okay and specify the URL you want to assign some is going to be the URL which we already specified that's a static variable day and you're gonna say object already does first okay alright so we don't have an object already how do we know convert it to a list of objects so to convert it to a list of object you simply use the array data array that's at least so it's going to disagree I'm simply going to return return arrays dot dot Astley's and specify the object array we want so it's gonna give us a list of objects so what's happening now is RS controller will have a URL pattern slash country we call / countries and our application is going to make it a price to this rest endpoint this urs endpoint of the way and get the list of countries for our so is this very easy to do so let's run this application and let go to slash countries and let's see that all happy some countries hopefully our mom is not anything so I'm gonna first I'm gonna save everything Sybil I'm going to run consume API so is running at ports 9000 so we go to port 9000 slash country's we should have a list of countries in our local application and we've actually conjoined a REST API so if this makes sense to you if you this is informative for you great like my video and also shade around and also let me know if you have any challenges whatsoever I will okay let's wait for a second so while we wait for a second I'd like to just display the procedure for the second pass now this second pipe or this second method do is call it methyl to I mean just say this is my thoughts - okay these make up - is that be mock robots but these I recommend you also Lenny because it's really very important so if I go to slice 9000 and close this so I'm going to http I go to localhost for 9000 slash countries so we expect to get so countries that we type there because I'm just being to trust countries so now if you have a list of countries exactly the same list of countries we have online we have now in our local application so for any rest endpoint you can consume it in this way let's now use the second method to get a list of countries so now we've used rests and plates to consume Olympia less now I use another method and this method is going to be to use Fame flying so let's go back to our application here so I'm going to kind of undo everything we did before so first I'm going to go to delete this restaurant place here because we are no longer using restaurant place I'm also going to delete everything from here okay perfect so I'm going to stop this application as well so I'm going to show you how to use another method tool to consume rest api so these where we are everything is still right from the scratch alright so the first now like us to do taking a look at the Presidio the first thing I'm going to do so these where we are we are edit the leak number two so let's go from number three so number three is that we need to add a service so I'm going to add a service that is actually going to expose the methods that we are going to be using so the methods we are saying we want to get a list of countries so we need get countries I also like us to consume this API here this list of product coming from port 8080 products I also want those economies let's have two methods get countries and get products I'm going to create a new a new service called a PRI service now normally you could have country service you can also have product service but I'm calling it API series now this MPI service is actually going to be on interface is going to be on interface and is going to have the methods that we want to expose some I'm going to say public list of objects because now if you are consuming an API sometimes you don't know what objects they are you simply know there is list of items and every item or every class in Java or derived from object so I'm going to say get countries all right so this method and we also need another one public list of objects as well in this case we have get through docks okay right okay so have these two items here so let me just import the class in fourth all right Java dot util dot Lee so this is what we have so we have this two methods we want to actually use to be able to call external API so let me show you like to display these so we have greater this interface is specifying clearly the two methods that we have read only calling to external and finds gets little countries and get mr. product the lesson want to do is to add another class this class now is a class that is going to sorry another interface is going to actually be API service proxy so if this Cali about the same thing is also a link of things but is going to exchange the previous interface API service so basically this is the class that will be responsible for actually the interface I'll actually make the request so it means that this interface here is going to be a thin client so make no mistake about it just take note that you need to undertake this week are same client they might need to give it a name let's give it a name let's say API service okay all right so so what it means is at this point this class now will be able to connect to any server that is specified how do you know tell these faint client what Sabah need to connect to that is where I need to explain to you you need to go to the application the properties file and expand say ribbon dot now in this case list check you have to say reborn ah let's see I like to yeah this way I am now ribbond that list of a list of servers list all servers and then specify people's then specify the list of servers in this case let's look at the list of servers in this case take note that we only need the Saba itself not the parts we need the survey itself and not the path so remember that I'm going to take the first lease of Saba the first item on paste it here and the second one we also was going to take the second one and again we take the part with the 17 are not the path ok I'm going to state control B so we have two servers right here so it means that the client will be able to connect to any of this server that contains the information that it is looking for ok all right so let's go back to continue the next thing we want to do now is to go to our controller file so let's go to this restaurant roller and then write the controller method right before they let me just go to check these application here ok perfect so in our controller now we need to write the controller methods the first thing we want to do is to explain the controller is going to expand sorry it implements the service API service proxy so this controller actually will use the API service proxy to make cost so I'm going to just implement the two methods yeah and in this the service in this place please you need to also say put the get nothing so this case in case of such countries you are going to put the mapping to now if we go back to the endpoint for the for the countries we can see that let me see okay so these end point that we are Charlie trying to mean is going to go to me so there is the end point this end point the parts now you are going to specify it separately unlike in the rest are in a restaurant place we specify the prophetís about the same both in using ribbon we are free but you using ribbon on thin client to specify them secretly in this way okay and in this case you also do the same thing are get marking to slash counting's so what's gonna happen whenever prices made to any of these now ribbon is going to look at the service and check which tower actually contain this information it's going to do this fight itself you don't need to worry about it just despite a parts and then specify all the stars of an accessory right so let's go back to the the API controller okay so now we have also specify get marking to to the case of countries I think with you yeah okay and in this case we slash products okay alright so now what do we return we are now going to use the we are now going to use this Savi's let's see let me just check so here we have the restaurant reliant remains the service okay we need one rotated to be a contact and they will faint client I'm going to I'm going to say us level 10 clients specify the base packages package classes and specify the client services quickest with proxy service API doc but ok perfect for these what we have right so the maximum want to draw this point is to we are now going to what do we now use to make this call so we are going to use the proxy the same client is what is going to be used now so it's kind of confusing but take some time to get your head around it's going to be really quiet so again we need we need KB yeah we need a being here to the wiring so you're going to be API service protein API service pro T these actually implements products service API service not API service project API service project API serviced probably think I'm okay let's just call it service probably well let's just call it quality just beyond just to make everything shot okay alright so we also need to add a constructor parameter so I'm gonna just click on this and that's fine so what I'm gonna write on here now is going to be very easy so I'm going to say I'm going to simply return prop Z dots get countries and here I'm gonna return proxy don't get product proxy object Kudo so it's as simple as that so the second method for some people this seem very very verbose and a bit more difficult but I think you need to learn how to use it because it applies most times when you are doing a built-in enterprise of which one this actually has more plumbing and possibly more security and ok so I'm going to yeah so I'm going to run it at this point hopefully I'm not missing out anything I'm going to run it so wrong and let's see if we have everything like before so we take now this one let's see if we have everything like before all right don't card started of course 1990 so let's go to our size / countries at 490 19 HTTP locals of 1993 nine nine zero nine thousand and this time let's try / products first and see what we have so no messages / products let's see I think we may have a fight for somewhere so let's get going to make VI / products phase no messages available much products ok so let's see yeah I'm sorry oh so this should be product not constant so that's my mistake so I'm going to save file save all and I'm going to restart right I think Tom cuts started at both 9000 so let's go again so I'm going to come here I'm gonna just refresh this page and you can see that it returns a list of countries how about it the how about the other one which is raised because not all so let's try the rest be 200 mark rest which would fall so I think we also have an arrow somewhere okay so you can see that I missed so the list of the sava here is not correct so I'm gonna just go to copy the correct server name which is actually actually this one which is the seller on time online so I'm going to copy and then replace this with ink so I'm going to replace it to remove these four not because this is just a demo and I'm also going to think how this trailing slash I'm going to save everything unless you're running to see all right so Tom Cass started again in port 9000 so let's go to try to check so it up of 9,000 I'm going to refresh and now you can see that we shows how we should have a list of items yes so we've consumed it so you can see the original part here but now we are consuming it locally in our application and the same thing we are going to also do of products as well so this is how to consume rest api using a thin client end ribbon does the second metal and make up one is using using the rest template I'm going to stop here I like to thank you for viewing please subscribe to my channel by hitting on the subscribe button below and please leave a comment like and share this video if it has been informated for you I remain kinda tech crew and I'm always there for you
Info
Channel: Kindson The Tech Pro
Views: 25,315
Rating: undefined out of 5
Keywords: Spring Boot, Microservices, Feign Client, Ribbon, Rest API, Web Services
Id: B792AiYpb50
Channel Id: undefined
Length: 26min 56sec (1616 seconds)
Published: Wed May 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.