Difference Between Put and Patch Requests in Spring Boot

Search for a command to run...

AI has transformed how I work as a technical writer. Large tech companies are developing and sharpening these tools, but some of them are better than others depending on what you want to accomplish. T

I've been writing professionally for 3+ years, and on this personal blog for 5+ years. Throughout this time, I've built a large network made of developers, other technical writers, solutions engineers

I’ve been writing online for almost five years, with a professional focus on technical writing for over two years. If you struggle to create technical content that is easy to digest, this article is for you. Where do I start? If you don’t know where ...

LinkedIn is the only social networking site purposely built to help job seekers. Over the years, LinkedIn has evolved by offering two types of experiences: Job searching Content creation This article will describe how to maximize your LinkedIn fo...

In the last 12+ months as a technical writer, I've worked on several projects highlighting three technical writing elements. This article will introduce you to: Information Architecture Docs-as-code Content strategy Information Architecture (IA)...

Have you ever wondered the exact difference between a PUT and a PATCH HTTP request?
In this article, I will provide you with some examples to show you the difference between a PUT and a PATCH request.
Let's start.
A PUT request is a request that updates an existing record.
This is what we have in our database:

Let's create a method that will update an existing customer in our controller.
@RequestMapping(method = {PUT}, path = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
public Customer updateExistingCustomer(@RequestBody Customer customer) {
return customerService.saveCustomer(customer);
}
Let's change the customer with id 16 (Steve Austin) with the name "Patrick David".
The Postman request will look like this:

And the request is going to update the database:

A PATCH request is an HTTP request that performs partial updates.
Let's see an example.
Let's add the method to perform the PATCH request in our controller.
@RequestMapping(method = {PATCH}, path = "/update/{id}/{phoneNumber}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Customer> updatePartialCustomer(@PathVariable Integer id, @PathVariable String phoneNumber) {
try {
Customer customer = customerRepository.findById(id).get();
customer.setPhoneNumber(phoneNumber);
return new ResponseEntity<Customer>(customerService.saveCustomer(customer), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
The best implementation that I've found to perform an HTTP request recommends using the ResponseEntity that will return the complete HTTP response (status code, body, etc.).
We search the customer based on their id.
We update only the phone number.
We return the new ResponseEntity.
If something goes wrong in the try, we return an Internal Server Error (= 500 status code).
Let's add a new record to our database.

The new customer appears into our database:

Now let's perform a PATCH request:

The phone number has now changed.

Now you might be wondering:
A POST request saves new data to the database.
A PUT request updates an existing record. If you've noticed, we send out the whole body in case of a PUT request.
A PATCH request updates some parts of an existing record. In case of a PATCH request, we only send the data we want to modify.
Often, I've noticed that engineers use POST and PUT interchangeably. I think we should use a POST request when we want to save new data and PUT only for the updates.
That's it for this article.
You can find the full Github repository here.
If you enjoy my content, please consider subscribing to my newsletter. 😊
I hope you've found this helpful.
Until next time! 👋🏾
ADDITIONAL RESOURCES: