Difference Between Put and Patch Requests in Spring Boot

Difference Between Put and Patch Requests in Spring Boot

·

3 min read

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.

What Is a Put Request?

A PUT request is a request that updates an existing record.

This is what we have in our database:

image.png

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:

image.png

And the request is going to update the database:

image.png

What is a PATCH request?

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.

image.png

The new customer appears into our database:

image.png

Now let's perform a PATCH request:

image.png

The phone number has now changed.

image.png

Now you might be wondering:

What’s the difference between a POST, PUT, and PATCH request?

  • 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:

Did you find this article valuable?

Support Maddy by becoming a sponsor. Any amount is appreciated!