Spring Boot REST Client to Consume Restful CRUD API using RestTemplate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone ramesh here in this video I'm gonna show you how to use sprinkle added errors template class to consume restful web services so we are going to basically develop a spring rest client to consume spring boot cool restful web services in one of my previous video tutorials that is spring boot my sequel GP have been addressed or could it be tutorial we have created a spring boot restful web services and we have tested it using portal magnetic line provided by Google and we haven't written a rest client to consume springboard code restful web services but in this video I'm gonna show you how to develop you know spring rest client to consume spring boot coolest pull-up services and we are going to use spring provided rest complete class to call Springwood cool rest epa's alright so let us see how to develop spring rest client step-by-step to consume spring boot coolest epa's alright without further ado let's get started so this is a spring would call rest pull up services project this project we have created in my previous video tutorial and we have basically created a fiber staps let me quickly review what are the rest tip is that we have created and expose in my previous video so we have created get our users rest api and get user ID create user update user and if accuser so these are the fiber rest endpoints we have created and exposed in my previous video and in this video i am going to write a rest client to consume these you know restful web services for that let's quickly create a class and let's name it as a rest client okay and let's quickly create a main method so here I am going to first list out the endpoints so for example so look at here this is the endpoint rest endpoint to get all users okay and let's create a few all the rest endpoints you so these are basically a endpoint URLs and we are going to consume these arrestee endpoints okay so let's quickly create here a rest template class object is template okay and what we will do here is we create a separate method to consume each the rest api okay so here I am going to write a method private static wide and I'm going to give a name as all get our futures API okay and inside that we are going to write the logic to call get all users press TPA okay so so look at here how I am doing so first we need to create a HTTP header okay so let's quickly create an object of HTTP header okay and the client is expecting adjacent from the REST API okay client is expecting JSON in a response for this API so for that what we need to do is we need to set except totally but right here call the accept and we need to pass a Jason because client is expecting Jason in a response so what we need to do here is we need to call arrays dot as list and here we need to pass media type media type and that is application Jason okay so basically kind expecting an Jason from the arrestee PA okay now what we'll do we'll Oh HTTP entity right when a constructor we need to pass multi-value map right so here what I will do I have a passkey as parameter also and value adds header okay so basically we are passing header to http entity here it's pretty simple right and next what we'll do we'll call a rest amplitude here so let me make this static so that I can able to access static free from the static method so this is the rest template thought we are going to call exchange method and we want to pass the URI here so URL here is get all users API in the method here is HTTP method that is get method and request entity that is entity and this one's type we are expecting Jason as a string so let's pass a string class here and look at here the exchange method returns a response entity right so let's go run let's create our response entity here and pass string and this is the result right we are storing result of exchange method in a result and we'll just print the result okay so this completes our you know a rest climb to call and get all users rest epa now let's go and let's run the spring but application and let us test get our users rest epa now the spring boot apllication is up and running on default embedded Concord server on port 8000 now let's run the rich client so here we need to call method from the mainland tonight so me and Matt touch here is min meters go right click for an ad in java application yes oh look at here in the console you can see we got a response or your objects from the rest EPA alright so this is how we write a rest client to consume get all users are h-tpa and we got 200 ads status code so let's go let's quickly create a rest client to call cat user by dear HT p.m. so look at here the endpoint URL so this APA requires ID as a parameter ID so what we'll do quickly create a method here private static wide and let's give a method as method name is call get user by D so you can give any name that you want Heidi API and here we cause ID so for that I am going to create a parameter op type map so showing teacher is ID value okay I'm going to call as param and this is the hashmap okay and we need to put the entry here so key is ID and values I'm going to call a user I am going to retrieve a user with ID 1 okay you know what we'll do we have a rest client instance right so rest template dot and get for object so just call get for object epa and here we need to pass the URL so URL is get user by DP a and response type is user dot plus and this is the param that's it and here what we need to do is we need to get a user object right okay and here I whatever I will just print the user object okay so let's learn let's run this program again and let us test whether this client who works or not run ads in java application so our spring beautification is already up and running so what is the error here user not found with ID 1 ok let's look at here they are a response we got user not owned with ID 1 so let's pass a different ID here I'm going to pass 19 so again I run the program yeah we got our user with ID 19 ok and they move is the user so now we have successfully developed a rest client to call get user ID rest EPA let's go and let's quickly create a rest client to call create user rest api so let's create a method again private static wild and let's call a method named as call create user API you can give any name that you want and here let's quickly create a user object ok and we have a rest complete object so let's go and let's call its method for post for entity and here we need to pass so there are many were loaded methods right so we need to call appropriate method here make sure we need to call let's pass the URL here so the URL is create user API endpoint URL and the request is user object and the response type is user dot plus okay and let's store the result of this method in the user object this is the user to let's go then let's print this user to okay so what we have done we have created user object times we have called post poor entity method on - template and we have just parcel this user object here and we just store the result of post for entity in our entity response entity object and we just I know printed the user object okay it's pretty simple now let's quickly go ahead and let's write a rest client for update and delete user h-tpa so let's write the rest client to call update user STP private static void and this is call update user API and then here basically we need to pass ID right so I am going to create a map here and this is ID 20 and here I'm going to create object of the user that I want to update so this is the user I'm going to topped it so I'm going to change from image to ROM and particle jaddo and I ran Ramesh to Ram at that gmail.com so this is the object that I'm adopted okay I'm going to name rename it after it you saw something like this and then again call a rest template dot post for entity and again pass the while here update user API endpoint URL and this is update user object and this is the user class object okay and post for entity expecting response at entity as a response so let's copy this and let's bring this user to the culture so this is how basically we update a user so again this is the straightforward method so let's create a method here call delete user API and here again we need to pass ID right so for that I'm going to just copy this map here and again call iris template it provided delete method and here we need to just pass the URL that is delete user API and here param that's all and direct method returns wide alright so here we no need to print anything so this will delete the user from the database so in a call update user API here we need to do a small correction so we have called post for entity method under template to call update user STP right so we basically need to call or put method here okay and let me quickly replace with put method and here we need to pass a param here instead of user class and this put method returns white so let me again remove the written here okay now let's go ahead and let's run this rest client and let us test all these arrest web restful web services all right so let me run this rest client right click run as java application and look at though look at the logs here so this is the response of get all user rest api and this is the response of you get user ID api and this is a 200 response for creating a user rest api and this is updating user HTP and this is the delete user HTTP we look at here the rest endpoints HTTP delete is to delete boot and this is the HTTP POST and this is the typical and this is the certificate and get all users all right so we have tested all the spring boot kueste players using spring rest client so this is how basically we develop the spring rest client using rest template class right I hope you found this video tutorial useful subscribe to my youtube channel whenever I will publish such videos will get notified and I'm going to host this was for on my github repository you know the description so that you can really time you can download source code source code from a github repository and you can do for it alright thanks for watching I will see you in the next video
Info
Channel: Java Guides
Views: 34,532
Rating: undefined out of 5
Keywords: java, javaguides, springboot, springframework
Id: ADtm_HXHyB8
Channel Id: undefined
Length: 16min 30sec (990 seconds)
Published: Thu Apr 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.