11 Using RestTemplate to call an external microservice API - Spring Boot Microservices Level 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what do we need to do we need to go to the movie catalog service the resource here I'm doing a singleton list but this will not do what I want to do is get actually there are a couple of steps here right the first step is get all rated movie IDs and then for each movie ID call the final step is to put them all together make sense now this is what I'm gonna be doing right the part where I'm calling the movie infoservice and getting the movie information back that's what I'm gonna be doing so I am going to hard-code the ratings for now okay the way I do that is first I'm going to get the ratings resource I'm going to copy this here sorry the model no copy this here because we need the model we need to store the ratings information I paste it here and then here I'm going to hard-code just that part and create a list of rating it's supposed to be done through a restauranteur so the communication happens through a test template but then what you get back is going to be string you need objects to deal with the data whenever you in Java you need to deal with data you need objects the objects needs to be of a particular class so you couldn't create the class here it happens to be the same exact class so I've just copied that over it'll return a string but then you can also unmarshal it hold that thought for a bit okay let's assume we are not doing rest template it's just hard coding it the whole thing the whole communication we hard coding what would be the first step you need to create a bunch of responses from the first API how could a bunch of responses from the second API intended so to hard code it you need classes so that's what I'm doing here so I'm creating an array off a bunch of ratings let's say 1 2 3 4 rating is 4 and then one more 5 6 7 8 it's creating this three okay let's assume this is the response we got from that rating data API right so somebody has come to us and said for this user give me all the movies is washed and details so we have to first get the movie C is washed that's what we hard-coding now that we have the movies that they have watched we need to get the details and for this you're gonna make the API call how are you gonna make the API call using rest template so I'm going to loop through this create a stream I can either do a for loop or I can do a map let's say I do a map so for each rating I need to replace rating with the catalog item all right so I need to get that information here collect collectors are to list all right make sense and now I can return this questions about what I'm doing here I've hard coded the ratings for each rating I should be making a call to the API but I'm not doing that I'm just creating a catalog element with hard-coded name and description these name and description should actually come from the API right I'm hard coding it and then I'm making it a list and then returning that get rid of this make sense now we're gonna change this to make a call for each of those movies I'm gonna make a call to movie info get the movie details and that's what the user needs to see all right so I can do that over here I'm gonna make this a block it's no longer a single line what do I need to do I need to first create an instance of rest template rest template is going to be the utility object which lets me make these calls okay so I'm going to say new rest M that I'm gonna let me create this instance over here at the top create a new rest template and the rest template has methods on it dot get for object okay what does this do it takes in two arguments the first argument is the URL that you want to call this doesn't have to be a micro service it could be any URL it's gonna make a call it's gonna make a rest call to it right and what it gets back it's a string right it also helps you unmarshal it into an object so if you know what the payload of that response is in this case we know it's gonna be movie info you can provide a class which has the same properties as the JSON and what this rest template is going to do is it's going to create that instance of the class populate those properties to it and it's going to give you a fully form object right all that is happening in this one line so it's two parameters the first parameter is the URL what's the URL the URL is the movie URL here it's localhost 8080 1 slash movies slash movie ID and then the second argument is the movie information so this is a payload which has two properties movie ID and name so I have a class sitting here which has just that so I'm just going to use it see here this is a class which has the movie ID in the name you can technically create an entirely different class it's perfectly fine but I'm just gonna copy this now some of you might be asking isn't it a bad thing to do copy paste of classes you have duplicate copies of the same class all over the place isn't that something that's frowned upon well in a monolithic application yes you don't have multiple copies of the same class but in the micro-service this is technically allowed why because you can't well you can not you can avoid doing this but let's say you want to create a library of all these model classes and then use that as a dependency in all these different things what's the drawback of that somebody wants to change that well you're gonna have to manage all those things and you you defeat the purpose of microservices being independent things I do you want to be able to deploy one without having to care about what the other team is doing if you use the same shared library you're bound the release cycles are bound all that stuff so it's perfectly okay to create copies of these classes the side benefit of this is let's say there are more stuff there in that class that's beneficial for that microservice but you don't care about that you can just use the fields that you want here so those classes usually in my experience even though they start out as copies since there's completely different things they end up picking different parts and the class is kind of diverged from there on and at that point people realize oh it's a good thing we created those copies so it's perfectly okay to create copies of these classes you can but then would it be sharing those interfaces because again you're gonna rent it you can you technically can but then again you're gonna run in yeah what if you need to change the interface and then you again have to dependency so it's it's a little less likely that you're gonna change interfaces than the core classes but then you never know yes if it's not backward-compatible you will have to let people know versioning is a whole different topic when it comes to micro services how do you version your micro services if you are adding a new field it's fine people who don't use it don't care and you can they can upgrade whenever they want but if you're removing a feel or what if you're changing the name of a field then you have to let people know or maybe create a new version v1 slash API right to put it in a different URL so that you know if you want to get the new version you have to call the different URL so it's a big it's a big topic in itself of your manage to versioning all right so back to back to rest template this is the line we were working on okay this has to go somewhere else it shouldn't be here it should be this call for each rating rated movie right so if it every movie that the user has watched we need to fetch the data but here this is the signature this is what needs to happen whenever you need to make a call this is what you need to do rest template don't get for object you're saying get me the resource and unmarshal it into an object it takes two arguments one is the URL that it needs to get and the second is the class that it needs to unmarshal to just gonna take that payload and then it's going to return a movie object right as easy as that so I'm gonna use this I'm gonna use this piece get rid of this here and what I need to do here is for every rated movie I need to take that movie ID and I need to call the movie information API with that ID okay so I'm gonna call this but not with foo all the time I'm going to append the rating dot get movie ID okay so for each iteration it's gonna make a separate call and it's what it gets back here is that particular movie so first time it's gonna make a call to movie ID one two three four second it's gonna make a call to five six seven eight yes you can and that's where the web client comes and that's where it gets into the reactive program and you can make area synchronous but then when you make it asynchronous you have to make this asynchronous as well because if you are returning a single object you have to block until you get that object and return it but there are ways in which you can return like a mono or a flux object from spring controllers so that you're telling spring hey I've set things in motion whenever it returns return it to the user and then you move on to do something else so you can technically do that if you guys are interested I'm happy to do like just not even micro-service is just a spring boot workshop where we just do a synchronous spring board so yeah that's that's a whole different way of doing things okay now that I have this movie object which is just the movie we want right we've picked the ID made a call just for the movie we want and now we're we can return the catalog item but rather than hard code the name I'm going to return movie that get me just making sense I don't I guess I don't have a movie description yeah I don't have a description so I'm just gonna hard-coded the description for now and then the rating is going to be rating target rating okay so this is where I'm putting those two together so this piece is the first API called the movie info this piece is the second API call to the rating database which we have hard-coded for now but you know the idea is to convert it into a live API call later right and then I do a collect to a list are you return it back restart this I think I have a knitter that's the semicolon no this one there's an error 404 seems very odd anybody ideas oh okay I see what you mean okay yeah you're right thank you this is 80-82 this is if you're doing a lot of bad things here I'm just gonna list out one what are the bad things we're doing but we have to make this work first cannot construct an instance okay so this is another thing that you have to watch out for when you when you have Java unmarshal something which is not an object to an object you need to provide it an empty constructor this movie doesn't have an empty constructor so I'm just gonna put that there the way the marshaling and an marshalling things work is Java fuss creates an instance and then parses the string and then populated one by one so if you don't have an empty constructor it doesn't have anything to create an instance and that's why it's complaining yeah yeah so I have I have an overloaded constructor so that's the problem okay and now if you see you get the values from the hard-coded ratings API call and then the live data from the from the API of course it's not technically not live data but it's making an API call so what it's doing is actually making multiple API calls right so there you have a list of two items for each item one microservice running on this separate tomcat instance making a rest call to another microservice running on a separate tomcat instance that returns back it unmarshal it and you're putting all the data together and returning it back and look at the amount of code it took to do this this is it it's like three lines of code so this is one of the reasons why Spring has become very popular it does a lot of things for you behind the scenes and other frameworks that kind of catching up as well
Info
Channel: Java Brains
Views: 349,921
Rating: undefined out of 5
Keywords: microservices, spring boot, microservices tutorial, java microservices, spring boot microservices, spring cloud microservices, spring cloud, spring, java brains, java, brains, koushik, kothagal, kaushik, spring microservices
Id: WPKv8NA-ZhE
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Fri Feb 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.