@PatchMapping

Learn how to perform partial updates to a record in the database.

Partial update

The PUT method updates the whole record. There may be a scenario when only one or two fields needs to be updated. In that case, sending the whole record does not make sense. The HTTP PATCH method is used for partial updates.

Sometimes we may need to update a single field. For example, once we enter a player in our database, the field that will most likely change is his titles count. The player entity only has a few fields and PUT can be used for update. But if the entity is large and contains nested objects, it will have a performance impact to send the whole entity only to update a single field.

So, in our example, partial request means that we only send the titles in the request body instead of the whole Player object. If we use PUT to send a partial request, all other fields are set to null. The code widget below illustrates the point if a PUT request with the following request body is sent to /players/1:

{
    "titles": 161
}

We get the following response:

{
    "id": 1,
    "name": null,
    "nationality": null,
    "birthDate": null,
    "titles": 161
}

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.