Inserting new user

Find the code snippet for addNewUser() operation as follows:

// to add a user
@PostMapping("/users")
fun addUser(@Valid @RequestBody userModel: UserModel): UserModel {
return userRepository.save(userModel)
}

The @PostMapping(path = ["/user/"])  annotation is the URL path of "/user/", and it is a POST request. Here, we enter the details of a user to insert the user data in the database.

To bind the request body with a method parameter, we are using the @RequestBody annotation.

The @Valid annotation makes sure that the request body is valid and not null. 

Here, we return save(userModel) to insert new user details into the database.