As part of the CRUD operations, let's also provide an update to our rest controller. Let's go to our controller here, I'm going to add a function here that returns a response entity of customer. I'll say update customer. Now as convention, we have to receive the ID of the customer for whom we want to make changes, and the customer data itself. It is possible that Id may be different than the customers ID, we have to discard the customers ID and then use this for updation. So the first thing I will be doing here is customer dot set ID of ID.
And then I will say your report dot save customer. And if everything is okay, we just want to return this one. What if something goes wrong, there'll be an exception and that exception will be written as status code 500 With a custom error message from Spring Boot. Now, as far as the mapping is concerned, we have to say here at the rate put mapping, we also have to say that this comes from a PATH variable. This comes from direct request body. So we'll say here at the rate PATH variable whose name is going to be ID.
And this is going to be from request body. And since we said that it's supposed to come from PATH variable, we also have to say your slash curly bracket, Id like this. So when you're making a request, you should say slash API slash customers slash some ID. And that ID will be mapped to this plus the payload will be converted into a customer object and will be received over here we are using the same function called save, but this time there is an ID. So because of which it's going to do an update if possible. And if there is any error This will be reporting status code 500.
Now first, let's go and get one of the customer, let's get the customer whose data is say 45. This is the data. And let's copy this, and then make a put request. Let's go to the body wrong. And then I will choose JSON and paste this. Now, even if you give some different ID here, it doesn't matter because this is the ID that will be considered for update, of course, the rest of the data will be there.
Let's change this from, let's say a phone number ending with 758820000. Some changes, let's go and make the email to be example.com. Let's also make this as let's say Dallas, and Texas. So now I can click on the Send to see that the data has been updated. Let's go and check it out. What is the response and you can see that the response came back after it has been updated.
So let's go also to verify in the DB. Let's look for 45 and then say run. And you will see that the phone number and with 0000 City has been changed state has been changed. The email is now accurate example.com so everything has been updated.