@PostMapping

Learn how to add a record to the database using a POST request.

In this lesson, we will create an endpoint for the REST API which creates a new player and adds it to the database.

The REST client will send a POST request to /players. The body of the request contains the JSON data for a player. Since this is a new player, the client will not pass the ID (primary key). The backend system will generate the key for the new record.

The REST service will convert JSON data to POJO and add it to the database. The primary key of the added player is automatically generated by Hibernate, which is the default ORM used by Spring Data JPA. The response to the client is an echo of the player details along with the newly generated ID value.

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