#16 Spring Web PUT and DELETE

πŸš€ Add to Chrome – It’s Free - YouTube Summarizer

Category: N/A

Building WordCloud ...

Summary

No summary available.

Transcript

00:00

now it's time to write the code for the update and delete so what we have done till now is basically we have this products uh page or the controller and we are sending a request to get all the products we are getting that on the client side it can be a browser or it

00:16

can be a uh API client like Postman and then we are also fetching one product details or we are able to add new products and we have done that in Postman so if I open my post man so this was the request right so we have sent

00:32

the get request using which you were getting the uh products we have also done with the post request and the post basically we are sending data as well so what we'll do is before we go for the update and delate let me add this product and then we'll do that and I can't do this now so if I send a request

00:48

it says the server is not running so to make sure the server is running so I will just restart the application and hopefully this time there will not be an issue uh because I'm recording after a long time and if you see here here uh it's running it's running on port number 8090 if I go back to postman first of

01:05

all let's say get the products and this time it will show only three because see we are not storing this data in the database every time you restart the application uh you will lose your data so basically we have the hardcoded link list or we have the hardcoded list which is in the service this is what we are

01:22

rning right this products now I want to add a product so if I click on the post which is having this data which is 104 and when I click on send you can see we got 200 okay but again if I say Get it

01:37

should be having four records and we have it so now uh let's try to update something let's say I want to update the price for this show mic I don't want the price to be 10,000 let's say let's make it 12,000 how do I do that see for the update what we have to do is we have to

01:52

change the type of the request first of all let me just have this data with me I will just copy this and next I will change the type of method so instead of saying get and post we'll go for put so put is used for the update and I will go to the body and I want to say I want to

02:08

change this data so this is the new data which I which I have okay so how do I make it work if I click on send now it will not work because we don't have any method which is accepting a put request so you can see it says method not allowed so how do we solve this it is

02:24

actually simple what we can do is let's go back to the controller and here let's create a method for the put so I will say public what I don't want anything return or maybe we can simply return the data or we can avoid it so we will say void void void okay that was not

02:40

intentional but anyway so I will say update product and here so when you say update basically you are sending the data as well right so we have to accept the data in the product prodad it can be any name doesn't matter and I want to use the request body annotation we have

02:56

used it for the post as well uh this is this is responsible to get data in the request and send that in this reference so here what we can do is we can say hey Service uh I got this request and I want you to update the product okay so I'm

03:12

doing that and I will simply say prod here and that's done but the thing is we don't have this method in the service so what I can do is I can just click here and say create a method update product and we got it this the tricky part when it comes to doing this manually with the

03:27

help of this list it's see in the normal world we store this data in database now because of a module called Spring data jpa it becomes very easy so we'll see what exactly spin at a jpa is in some time in the upcoming videos but here we

03:43

have to do some manual work but trust me once we start with sping jpa we just have to change one line and it will work okay but then here I'm not sure about the code let's try so I will say products dot uh I don't want to add now

03:59

I just want to update so do we have a method which will update this unfortunately we don't have it so we have to go in a manual process so if I want to update a particular list I will say get can I set the value to Yeah so basically this is what we want to do we

04:15

want to set the product on a particular ID but I don't know the ID where to set this right that's a tricky part so the way you can get the ID is this so you can run the loop maybe I I can use a normal Loop here I can just run the loop here from int 0 to I less than products.

04:35

size i++ I just want to get the ID right or the index so I will say int index I want the index index will be originally zero um but this is a problem here right so okay we'll talk about the problem in some time basically I want to iterate

04:51

and if I want to check if the products. get I do get product ID is equal to dog product ID if this is matching I will set index to that particular I value right since for only has one thing we

05:08

can just remove the cly brackets and I got the index and the product so basically what we have done is we are setting So when you say s it will replace basically we are changing the data or the object with the new object which we got here so we have we are

05:23

writing s such such a ly code right if you have a better solution let me know in the comments this is not the best solution but once you start with data jpa this will become easy okay uh I know I'm creating suspense here but you will see that later and let me know if you have a better solution to update the product list okay will this work let's

05:39

try first of all I want to map it also so I will say put mapping and here I will accept the request for products the same list or the same URL but then with a different uh method and once we have done that I will just refresh this the problem is on once I restart the

05:55

application we are losing that fourth uh object because again we have to add so let's say we only have three records and now if I go back to my Postman let's send the get request first of all and see what are the data we have so you can see we have this data and I want to change the let's say this time let's

06:12

let's change the second one okay and let's change the price for this and for this I'll be sending a put request you can see we have a put request for the same URL and the body I want to change it with this but then the instead of saying Canon camera I will say Canon

06:27

only let's remove the camera and says send okay it says unsupported media type okay if I check the data okay let's debug this so what I will do is I will just try to print something here and I just want to check if this is getting

06:42

called not sure what is going wrong and click on send okay same error but is it printing that now it's not even coming inside this so you can it's not printing I'm here that means the update method itself is not getting called oh I think I got the problem so I'm sending text I

07:00

should send Jason maybe that was the issue oh such a simple problem so I was thinking about something else not looked at Json here it was text and now we have solved it so you can see we got an update says 200 and now after making the changes I will say get products and boom

07:18

can you see that in the second data we only got Canon now we don't have Canon camera so this is working so put we have done the next step is to do delete so now how delete will work so basically for delete we have to send a delete request and okay question arise how we

07:35

are going to delete this so we say product slash and then we'll give an ID so instead of sending the object to delete we can send the ID product ID so maybe one1 one2 103 let's say I want to work with one2 and I want to delete this so when I click on send it should delete

07:52

that particular object or the product from the from the products okay so now it says method not allowed that means we have not the mapping yet so let's do the mapping so here I will just create a method so public void or maybe if you want to return something you can do that

08:08

or you can return the status but at this point I'm just trying to keep it simple so I will say delete product and here instead of accepting an object we have to accept the value uh the same thing we have done here right so we are accepting a particular value and we can do that with the help of path variable so let's

08:24

do that here so here I can say I want to store that in a value called product ID but we have to use an annotation called path variable to accept the value and then the mapping should be okay what do you think what should be the mapping the mapping is delete mapping uh we have to

08:41

specify the URL which is products slash and then we have to pass the number right so we have to accept that with the help of this cly brackets and we have done that you just have to make sure that this name is similar to this otherwise you have to mention in the bracket what new name you're using since it is same we don't have to do do that

08:57

okay next how do I delete it I don't want to delete that from a controller controller will send send a request to the service service will say okay it's my job to delete but I don't have this method so we'll create that method so I will say delete product accept the product ID and delete it but

09:12

unfortunately we don't have this method in the service so I will click here and say create a method in service now how do you delete so we got the prod ID but we are not sure the index number because if you want to delete from the list which is products dot if you say delete

09:27

or if you say okay what's the method for deleting remove so we can say remove and we have to pass the index number okay but I don't have the index number so how do we get the index number we can use the same logic or maybe we can create a common method where you can pass the

09:42

product it will give you the ID right or I can just use the same code multiple times not a good idea okay this is not good I'm repeating the code so basically uh I'm not following the drive principle if I'm if I'm correct do not repeat yourself I'm repeating myself but you

09:58

got the point right so you can basically create a method which will accept the object or the ID and it will return something but in this case I'm not getting the object I'm getting the ID so first we have to get the ID so from this product ID I just have to match with this so instead of saying this I have to

10:14

say product ID and here I can pass the index okay so what I'm doing is I got the product ID I'm running a loop to check if the product ID is matching with any of the product ID if it is matching then remove the index see one of the problem I was talking about here as well is is what if the it which you are

10:31

passing is not found in that case what it you know what it will do it will delet delete the first element uh technically you should return by saying that the item not found but at this point I just want to keep it simple and whatever idea you mentioning I'm expecting this should it is there uh so

10:47

it will work but again not a good code not a perfect code but here we are not we are not here to work with the logic we here to understand how spring boot Works how spring MVC Works how the layer works right logic is not that important at this point just to keep it simple but you got a point I'm showing you the

11:02

problems as well you can solve it and if you have done that uh let me know in the comments what solution you have used okay so this looks goodt uh I'm not sure if this works and that's how the development life is right you're not sure what code will work and what will not work so you run it and say it so let

11:17

me go to my Postman first of all let's get all the products you can see we got three products and now I will send a delete request send no problem we got the status code AS 200 and now let's send the get request

11:34

once again to see how many dat we have we only have two now you can see one2 is gone so that's how basically we achieved the CL operations and that's what we have done right so we have done with the get request initially we have talked about the post as well now we have done with the put and delete so that's how we

11:50

achieve with the c operation using springboard so yeah that's it from this video where we have worked with the other two methods the put and delete let's see in the other upcoming videos bye-bye