@PutMapping

Learn how to update a record in the database using a PUT request.

In this lesson, we will create an endpoint to edit a player record and update his information in the database.

The HTTP PUT request is used for updates. The REST client will send a PUT request to /players/{playerId} with JSON data containing the information to be updated. The player’s ID is a path variable.

The REST service will convert the JSON data to a Player object, and using the id of the player; it will overwrite the player’s record in the database with the one sent in the PUT request. On success, the REST service will respond with the updated player record (which is an echo of the request).

Press + to interact
PUT request to /players/4
PUT request to /players/4

Service layer method for updating a player

To handle the update based on the player’s id, we will create a method called updatePlayer() in the PlayerService class. This method takes in two arguments: a player’s Id and a Player object. It returns the updated Player object.

public Player updatePlayer(int id, Player p) {
//call repository method to update the player
}
Service layer method for updating a record

The primary key passed in the method will be used to fetch the existing record from the database. We will ...

Access this course and 1400+ top-rated courses and projects.