DEV Community

Cover image for What's the difference between a PUT and PATCH request in Spring Boot?

Posted on • Originally published at

What's the difference between a PUT and PATCH request in Spring Boot?

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.

If you haven't read my previous article about " How to Create a Spring Boot REST API", please take a look at that as this article will operate as an extension.

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:


In our controller, let's create a method that will update an existing customer.

    @RequestMapping(method = {PUT}, path = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Customer updateExistingCustomer(@RequestBody Customer customer) {
        return customerService.saveCustomer(customer);
Enter fullscreen mode Exit fullscreen mode

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:


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();
            return new ResponseEntity<Customer>(customerService.saveCustomer(customer), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
Enter fullscreen mode Exit fullscreen mode

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:

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, check my Hashnode blogs. 😊

I hope you've found this helpful.

Until next time! 👋🏾



Top comments (0)