@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}
Run the application below to test updating a single field using a PUT request with the provided API widget.
package io.datajek.tennisplayerrest; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class PlayerController { @Autowired PlayerService service; @GetMapping("/welcome") public String welcome() { return "Tennis Player REST API"; } @GetMapping("/players") public List<Player> getAllPlayers() { return service.getAllPlayers(); } @GetMapping("/players/{id}") public Player getPlayer(@PathVariable int id) { return service.getPlayer(id); } @PostMapping("/players") public Player addPlayer(@RequestBody Player player) { return service.addPlayer(player); } @PutMapping("/players/{id}") public Player updatePlayer(@RequestBody Player player, @PathVariable int id) { return service.updatePlayer(id, player); } }
Ensure the application is running in the widget above before sending HTTP requests using the API widget below.
Collection
Key | Value | Description | |
---|---|---|---|
RESPONSE
Enter the URL and click Send to get a response
The titles have been modified but the rest of the values are now null which is not the desired outcome. The PUT method requires the entire object to be sent, even when we want to modify a single field. If partial data is sent, then all other fields are set to null. PATCH comes in handy in such situations. It allows a list of changes to be applied to the entity, as we will see in this lesson.
Service layer method for patching
In the PlayerService
class, we will implement a method to handle partial updates to the Player
object. The method patch()
will have two arguments. The first is the id
of the player on which the ...