π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
so basically we were able to get the products right so if I go back to the browser and if I hit the URL for the products it will give you the list of products of course I can predy it and you you will get in this format now basically you got all the thing which is your server return to the client of
00:17
course the data now basically we fetching everything right and that's what you can see here what if you want to fetch one what if you want to add something in this list now this is something we are going to do in this video but before we do that uh I want to talk about the other type
00:32
of API tool which you can use not just your browser because see when you talk about rest API and if you want to basically work with data what we normally do with data is we send data so you have your client here and then this client will send data from uh client to
00:48
server then it will also accept data from server to client what we are doing here is basically we are sending the data from server to client right but we do certain other things as well we send data from client to server right to store something in the database if you
01:04
want to do that uh sometime we want to so basically we are fetching and we are storing right so we got two things Fetch and send or store next what if you want to update what if you want to delete certain things example the product here
01:20
I want to maybe I want to delete this 103 maybe I want to update this particular price here so this thing you can do with the help of some type of request now rest API basically uses a protocol called HTTP we have talked about it before and it has certain methods to work with so basically your
01:37
rest API uses a protocol called stdp which has certain methods to work with so if you want to fetch something we have a get method if you want to store something on the server we use post if you want to update something we use put
01:53
if you want to delete something we have delete apart from this we have other methods as well but these are four we normally use now we also use something called a crud shot which is for create read update delete so we can create with the help of post uh we can read with the help of get
02:10
we can update with the help of put and we can delete with the help of delete so these are the methods which is available so rest uses HTTP and we are going to use this now by default when you say when you pass a URL in the browser it sends the get request now this is what
02:25
we have done but what about postp delete now this is not something can do with your browser right so you can't simply use your address bar to change the method name now this is where we have to use certain API tools to do that one of the tool we have multiple tools one of the tool is Postman so if you know how
02:41
to use one tool it is easier to explore the other as well so here what we going to do is we are going to use this particular Postman uh if you want to get this so let me just talk about this so this is a postman this is the logo looks like so if you search on the Google uh I
02:57
can also get take you to the website this is is the official website of Postman you can get it from here uh it's free it they also have a paid version but we don't want it we are going for free version here and it works okay of course I could have used some Advanced recent tool but that's fine if this
03:14
works so how exactly we going to hit the URL it's very simple you can say Local Host colon in 090 that's a port number we are using uh Slash then you have to say products now if You observe we have a method called get here right apart from this get if you expand there are
03:29
multiple methods we have postp put patch delete head options we are going to use only four so let's say we have a get request and when you say when you click on this send button it will send a request and you will get the response so this is a response you got apart from response you also get a status code here
03:46
which is 200 now there are different status code options available uh in the series of 100 200 300 400 500 if you have seen one of the most famous uh code which you get is 404 for resource not found uh so that is a part of this status code
04:02
it it comes under 400 series so if something is wrong with the resources it will send you 400 series if something is wrong with the server it will go for 500 series if everything is going well it will go into 100 Series so that's how we
04:17
have Defense series and we'll talk about it this series in the upcoming videos but we are getting the status code as well this is a response time which is 200 and yeah and this is a size of data which you're getting back so basically the this is your data here apart from the body you also get some headers from
04:33
the server uh this is the header so the type of data you're getting is Json that means you can also ask for the XML data if you want but by default it will send the Json data so let me go back to the body and you got three response if you have more you will see more uh you can
04:48
also check this in text format and this is how it looks like but we want to see that in J format which looks good and that's what you got here okay so this is what we have done to get data but what about you want to let's say get one not all one something like this I want the
05:04
products for sure but let's say I want the product with the ID 102 can we do that so we can simply say sl102 this is how basically you also build your rest apis right so you simply say products which is URL for all the products and Slash the ID which is the which you want
05:20
to fetch one2 and when you say send it should fetch only one record now this is not working and you can see we also got a status code called 4004 or 4 before uh the resource could not be found we have talked about it and this is the error which you got again not a good-looking
05:36
error but that's fine uh it says not found let's solve this I want to return one data how do we do that so I want to hit this URL now to achieve that we have to create one more method which will accept this particular URL products will only uh accept products not the Slash
05:54
and the number so what I going to do is I will say public and this time you're not going to return the list of products we are going to return only one product because we are accessing the uh unique field which is product ID so I'm going to say product return one product and
06:09
instead of saying get products I will say get product by their ID so we can just use this name you can also say product get product that works I will say get product by ID and here I will accept the ID so whatever ID I'm I'm sending from there
06:26
so I will say prod ID whichever whatever you're sending from the client and based on that ID I'm going to fetch so who is responsible to give me that so I will say service hey service it is your job so I will say get product by ID I want this method in the
06:43
service layer as well U and then I'm going to pass the ID now unfortunately we don't have this method called get product ID or get product by ID in the service layer and if you see the service class let me just close the application
06:58
properties we don't need that now now if you go to the service class or product we only got one method here right which is get products I want this as well so I can ask my ID to give me that so I can just click here and say create method and we got the method now but it has to
07:14
do the actual work right now this is tricky how will you get this particular data now the data is something in the list right which is in this added list so I can say return and this has to come from the products so we say products. getet now I'm not sure
07:30
in which particular method I can get that I just want the product based on their ID right so basically what we can do here is we have multiple ways we can use normal for Loop enhanced for Loop to get the data or I can use the Java stream API if you not sure what same API
07:46
is you can find the link in description if it is not there let it me know in the comments I will just put that so what I will do is I will say hey I got the list of products from this products I want to create a stream because using stream you can filter the products so I say filter
08:01
dot uh I mean stream Dot and I can apply a filter now filter based on what for any product which I get from the stream If the product get product ID if it is matching with the product ID okay the moment we get this and I don't I I just
08:18
I just want to stop this search once I get this product and I will say find first because stream has no idea that we we have a unique product ID stream says okay I'm I will filter this but there might be chance that you will get multiple so I will say find first to get
08:34
the first element and then find first dot get now this will return you the product okay so this is the logic of course you can also use normal for Loop and if you have that logic with you put that in comment so that others can say it and if you're not sure what stream API is you will get the link in
08:49
description okay so this implementation is done uh in the controller I'm asking the service to do that for me and now I want to check if this is working let's restart the application uh okay it's still working and done now where do we test it so of course I can do that in the browser so I would say products
09:05
slash I want to mention one3 and we got the error okay still not working something is wrong now what is missing here most important thing we are missing the request mapping so the request mapping has to be done for the
09:23
slash products slash 4103 right we have to do the mapping there and now after doing the mapping of 103 let's see if this works and this time I'm going to test that in the postman so I will say send the request for 103
09:39
send okay it's still not working it says optional uh in parameter PRI is present but cannot be translated to null due to decare as a primitive type okay so what's the next problem okay first of all there are two problems okay I wanted to show you that problem but then then I
09:55
realized I have to first talk about this one see the problem here is you are passing you're you're writing one3 right which is the harded values don't you think we are not going to search for 103 always we might want to search for 102 or 101 or 1010 later how
10:13
we going to change that so I don't want this to be static I I don't want this to be dynamic so what you can do is you can use a syntax called you can put a curly brackets here and you can put your variable name or the some text there which is per ID not the number now what spring will do is whatever is is coming
10:29
in the browser so which is this it will convert your 103 or it will store your 103 here it will map it and that has to be mapped here in the prod ID apart from this you have to also use a annotation called path variable because we want to
10:45
match it now so you want to match your prod ID with this prod ID so you have to use this annotation called path variable okay there are lot of L anotations will be coming up and once you get used to it you will remember but since we are accepting the value from the path of the
11:01
address bar or the address The annotation name is path variable and by making those changes we have solved two problems and now let's see if this works with 103 send it worked for 102 send it worked for
11:19
101 it worked for 105 which is not there it will give you the usual uh error but then this time it's not saying that F it's not found the request has been sent it's just that uh the data is not there right U you can also solve this you can hand this handle this exceptions by uh
11:37
handling it on the server site but we're not going to do that now okay so this is what you got uh you can do one more thing if you want uh in the service when you say get here you can say find first dot or else you can pass a new product
11:53
with a dummy value let's say one 100 no item and the price is zero so so what we'll do is if the item is not found because 105 is not there in this right so at this point you can return with uh no item so now if I restart the app there is one way of one way of handling
12:10
it not the best way but one way now if you send request for this you will see you got 100 no item zero price okay but again not a good idea or not the best solution so what we have done now is We have basically fetching the data with the help of Slash with the product ID
12:27
next maybe I want to send data and store that in this uh in that in this list here so how do I do that what I want to do is I want to create a method which will add the product so basically we have to send this product from the client to server so I'm doing that in
12:42
the service now so I'm going in the reverse order uh for the get we have we went from client to controller controller to service this time we are coding from service so I want to basically first of all accept the product and I will say PR here and I want to save this product in
12:58
the list the list name is products dot I want to say add and I want to add let's say I mean I want to add this product okay so this is done U now we have to make the changes in the controller so in the controller as well I will create a method which will return return nothing
13:16
and we can say add product which will accept the product from the client and this is beauty right because on from the client side you are going to send Json on the Ser side it will be getting converted automatically in the object format in fact if You observe when you
13:33
are sending the list of products or even a single product you sending the object format right and what you are getting on the client side is Json who is doing that someone is converting the data from java object to Json from Json to Java object and that's where you got a
13:49
library here so when you add a spring web you buy default get one of the library called Jackson so if you expand this there's a Jackson Library here now this Jackson library is responsible to convert your object into Json from Json to object so it does that for you so I
14:05
will say product prodad and we can simply use the service layer service. add and pass it so if You observe we are not doing anything in the controller right controller job is just to accept the request if you want to do certain things ask your service cl to do that you don't do it right so this is fun but
14:22
we have to do the mapping as well so I will say request uh request mapping and this time we'll do it for the product so we're not saying products we are saying product or we can also say products that's fine sending a request for the products right but here's a problem if
14:38
you see this products and this looks same right that means for two different methods we using the same URL will it work it will not there will be confusion right and that's where you have to you can use those methods remember post get
14:54
but we have not used this method right this is get request this is also a get request we have not mentioned it is because by default the methods are get get methods if you want to use something else we have to mention that so you can give a comma here and you can mention
15:09
the method type or uh you can use some specialized annotation here so what you can do is instead of using request mapping you can say get mapping so here also instead of saying request mapping you say get mapping and for post instead of saying
15:27
request mapping we are going to use post mapping now these two mappings are different is because of their methods the URL looks same this one and this one but the methods are different right so the controller and service done now let's try to work with the client side see for
15:44
the Post request we can't simply use the address bar you can create a react application using which you can send this data otherwise you can also create a form but again you have to do that in the JavaScript you have to do some code to achieve that but we're not not going to do that we're going to Simply use Postman so after making these changes
16:00
I'm going to restart the application and I I think it might fail because of the way you have defined the list let me just check that later uh let's go back to the postman and now instead of sending a get request we have to send a post request what is a URL the same URL the products right we're not changing
16:16
the URL between the get products and uh add product we're just changing the method here uh this is a response we not supposed to touch the response we have to touch the uh URL I will click on send and you can see we got an error unsupported operation uh okay it's it's
16:32
with the list right so if this is the error I'm talking about but okay we'll talk about this ER later but one more thing we are not sending any data right so basically we have to send the data as well in the body here okay now how will you send data it's very simple you click on the raw here and select Json because
16:49
you're sending Json data and you can enter your Json data here but if you're not good with Json that's fine what you can do is let's send the request for 101 uh so this is a history you can just get your old URL and you can hit that so we got this I will just copy this and uh
17:07
go back here for this for the Post request go to body raw and Jason put your data here and update it so this is 104 I want to create a new one let's say um what I have in my on my table
17:24
let's say AC remote and the price is let's say 2,000 I don't know how how what's up cost of AC remote so let's say we have 2,000 and now we just have to send it again we got the same problem so we are sending data there's no problem with the sending data the problem is I
17:40
think when you create as list it creates a immutable list so what I'm going to do is I'm going to say new add list and I'm going to pass this add list inside this so it will make it mutable it's not able to add data so new add list that's what
17:56
I added here let's restart the app and now hopefully this will work I will say send okay something worked we have not gotten a response because the in the controller we are using void right is not returning the data but you got status it says 200 how do I verify this
18:13
that's tricky right the way you can verify that is by sending a request for the get let's get all the products and okay no it's not working you can see we got zero null and zero it's not getting added uh what's wrong let me just check
18:30
my controller I know the error I got to know the error but I want to show you that what I will do is I will just try to print whatever we accepting on the client on on the server side and let's see what you are getting that will solve the problem so server is
18:46
start done I will send the same request which we have done before the body is still intact I will say send we got the same response but if you see the okay what my bad my bad my bad now since I thought we are using longbach which we are not doing that I should have added
19:02
two string method because in lombok you get that by default now since I'm using Longo from a long time it normally works so I will set to string and restart it was printing the address I don't want the hash code uh let's say
19:17
send and this is what you're getting on the server so that means what you're sending from the client is not actually getting received on the server side it's because to achieve that we have to use one annotation called request body now since you are sending a body from the
19:32
client to the server you have to use this anation called request body now this will try to match the data which you're sending and put that in the product uh the prod thing here okay I hope this time it will work I hope and you learn from others right you know now
19:49
why why certain things why we are using certain things the way we are and now send same response but on the client side if you see or on the console if you see you you are getting the data what you're sending from this from the client and now uh let me send the get request send go down go down and you can
20:08
see we got 104 good right the only problem is we are using a list in the code it's not sh in in the database the moment I restart the application that you will lose this data but anyway the point is we it's it's working so now we know how to use get we know how to use post we also know
20:25
how to get the data using one how about put and delete let's try to do that in the next video so to this point what we have done is now we know how to use Postman to work with the apis uh we have sent the get request and post request
20:41
it's fun so just want to show you the product controller once so we got three mappings uh get mapping get mapping and post mapping right so how do we do put and delete let's see the next video see you the next video bye-bye