Learn how to intercept HTTP PUT, PATCH, and DELETE commands with RestController methods.
- [Instructor] Now we are able to implement HTTP PUT, PATCH, and DELETE. The semantics of PUT is to update all the attributes of an entity. The semantics of PATCH is to only update some of the attributes. The methods are similar but we must respect the intent of the API. We're also going to implement HTTP DELETE of a tour rating.
So we're back in our tour rating controller and I have another helper method to verify the tour rating given a tourId and a customerId. Look up to see if there is a unique tour rating for that. If there is not, throw a NoSuchElementException. If there is, return that rating. So first, let's create update with PUT.
There our path variable again and our RatingDto which is our request body and also requires validation. RatingDto_ratingDto. Let's put in our request mapping, method = PUT. So first we're going to look up the TourRating, given the tourId and the customerId.
Then we are going to get every single attribute of the tour rating and save it to the current rating. We're going to set the score from the RatingDto. Set the comment from the ratingDto comment. Then we're going to save it and return it as a RatingDto.
So the RequestMethod.PATCH is almost identical so I'm going to copy and paste it and just modify it as needed. So this will be PATCH, updateWithPatch. Here I'm going to check on each of these. So if (ratingDto.getScore() is not equal to null, then set the score.
And if (ratingDto.getComment() is not equal to null, then set the comment and then persist it and return the Dto. Now let's implement DELETE. We're just going to copy this to make my life a little bit easier.
Then we have a new path variable called customerId. Set the request mapping method. RequestMethod is DELETE. We have something extra on the path which is customerId. Okay. So we're going to look up the TourRating, given the tourId and the customerId.
And then, delete it. Let's exercise those APIs. So first, let's create a rating. Let's say that we had a customer number four and he submitted a rating for a tour and he thought it was bad. So we create that. On further thought, he said, "Well, maybe it wasn't bad.
"It was okay." So this case, all of the attributes will be updated. Then the client realizes later that the comment was updated but not the score. So we can move the comment as we're only updating the score and changing it to three. That would be a patch. Then customer four comes back along and said, "Well, forget it.
"I'm just going to delete my rating all together. "I can't make up my mind about the tour." I need to add something on to the URL and that is the customerId. Great. Let's revisit our use cases to see if we have met all of the requirements. So for use case one, rate the tour. Our HTTP POST, PUT, and PATCH methods on the ratings endpoint plus automatic data validation meet this requirement.
For use case two, viewing all ratings. The HTTP GET method on the rating endpoint meets this requirement. Use case three, view the average score. The HTTP GET method on the /ratings/average endpoint meets the view average score requirement. Use case four, follow REST standards. Throughout the REST controller, if a method is invoked, that depends on entities to already exist or not exist, we throw a NoSuchElementException.
The return 404 method traps the exception then sets the HTTP response code to 404. The PUT, PATCH, and POST follow the intent of REST even though they all call TourRatingRepository.save.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.