How rest API stores the data?

Suppose we have printed the employee from data on console all right guys we have successfully created a form and we have successfully got the data of the form while form submission in next video we’ll create the rest-client which we call a rest API and we submit this form in a database we just call our HTTPS and we send this form data to the rest API and rest API will internally store the data in MySQL database alright guys in the previous video.

We have created add employee form and we have stored from data in first name last name in email ID properties and we were able to get the data from these properties while from submission okay so look at here I just entered the first name last name email ID and when I click on save button I can get a form data in save employee method okay now what we’ll do we’ll make our spa call and we send an employee data to the rest API and rest API will store this data in a MySQL database we have already created and exposed ad employee rest API in one of the previous videos so we are going to call create employee rest API and we will send this data to the rest API.

In order to do that first, we need to create a rest client using Axis HTTP library go to the employee service dot JS file and we have already created a get employee method which will call all employee rest API okay now similarly let’s go ahead and let’s create one more method over here like create employee and just pass employee object as a method argument and then return Rios dot post so we are going to call post method here because this is the form submission so once we submit the form we have to send from data to the rest API through first HTTP method and then rest API will handle a form data at the server-side and it will store the form data in a database all right.

Let’s pass a base URL over hereafter that we need to pass a form data that is an employee that’s it next go to the creation employee component okay so go to the save empire method inside this method we are going to call employee service and then call create employee and pass employee object, so Axioms post method returns a promise so let’s go ahead and let’s call then method and then response and inside this then method what we’ll do we’ll navigate to the employee’s list page so once we submit a form data successfully then we should navigate to the employee’s list page right for that here what we’ll do we’ll just call this dot props dot history let’s get a history object from the route we’re using props we got a success response from the rest API.

We should navigate to the employee’s list page okay great let us save all the files create component create employee component as well as employee service and then let’s go back to the browser and let’s see how it works yeah here we go just refresh one more time and let’s add data to the form first name is Ramesh last name as further email ID as hit save button yeah here you go so we have successfully added a new employee to the employees list all right guys what we have done we have created employee method inside employee service, so this will make a rest API call and inside a creation employee component we have used employee service and its method called create employee.

We just pass employee object to this method and after the successful response of the rest of API we just navigated to the employees list page, it’s pretty simple right great in next video we will create few rest APIs like get employed by ID rest API and update employee rest API and then after that we will create a React components to consume the rest APIs alright guys let’s create get employee by ID rest API get employee by ID rest API okay just type a method public and this method returns the employee object to the client.

Let’s give return type as an employee and let’s view method name as get employee by ID all right and we are going to pass ID as a method argument right long ID let’s annotate this method with at the rate and get mapping annotation let’s configure the rest endpoint URL over here employees slash I’d be okay, so this is the path variable now we want to map or we want to store this ID value into uh you know the java variable so for that we are going to use add direct path variable annotation to map this ID with this variable so let’s use at the rate path variable annotation now let’s retrieve an ampulla object from the database for that we are going to use the employee repository.

Let’s create an employee object here and let’s use employee repository and we’re going to call by ID right find y ID method and just first ID ID to the find by method so look at here find by ID returns optional as a written type so here what we’ll do we’ll use at the air dot orioles throw method so if record not exist in database then we are going to throw resource not from the exception for that let’s use orioles throw method over here and basically we need to pass a lambda expression to this method because this method internally uses uh functional interfaces.

We use lambda expression to implement a functional interface let’s use lambda expression syntax here and just pass the object of the custom exception that is resource not found exception a resource not found exception and just pass the custom message over here employee not exist with ID and just pass the ID over here let me break this statement okay great so what we have done we have we have called find by ID method, so this method basically returns optional uh object as a return type and optional provides or else through method.

If you record not existing database then we just simply throw resource not from the exception like this now let’s return an employee object to the client so what we’ll do we need to also return HTTP status right for that we need to use a response entity class let’s use return type as response entity and this is the generic class so let’s type let’s pass employee as a generic type and here let’s return response entity and we are going to return ok status that is 200 status so let’s choose ok and just pass the empire object in a body that’s if it’s pretty simple right all right guys this is pretty simple rest API that is uh you know get employed by ID rest API.

Let’s test this rest API for that what we’ll do I will start the application go to the spring boot application main class right click run as spring boot app now look at here springboard application is up and running on embedded tomcat server on 4080 now let’s go back to the post manage client and let us test this rest API open a new tab and just type localhost address zero slash API slash v1 slash employees and here we are going to retrieve a particular employee.

Let’s go back to the database so I am going to get an employee Ramesh with ID 1 so let’s pass ID 1 as a path variable slash 1 and choose HTTP get method over here and simply hit send button, so this will retrieve an employee object with IDE one if you want to retrieve employee with ID 2 just pass ID 2 over here and just hit send button, so this will let you my employee with id2 all right guys we have successfully created get employee by ID HTTP and we have tested using postman rust client.

Leave a Comment