The controller class will map the URL path of the project. Here, we will use the GET or POST HTTP request functions to create the REST API. Here's a sample code of the controller of the project, named UserController.kt:
@RestController
class UserController {
@GetMapping(path = ["/users"])
fun userList(): ResponseEntity<List<Users>>{
return ResponseEntity(getUsers(), HttpStatus.OK)
}
private fun getUsers(): List<Users> {
val user = Users("1","Sunnat", "[email protected]", "0123456789")
val user1 = Users("2","Chaity", "[email protected]", "1234567890")
val user2 = Users("3","Jisan", "[email protected]", "9876543210")
val user3 = Users("4","Mirza", "[email protected]", "5412309876")
val user4 = Users("5","Hasib", "[email protected]", "5678901234")
return Arrays.asList<Users>(user, user1, user2, user3, user4)
}
}
Here, we've created a user list of five people with the user model. In a controller, the @RequestMapping annotation is applied to the class level and/or the method level. This maps a particular request path onto a controller. With the @GetMapping(path = ["/users"]) annotation, the client will send a GET request to get the list of the users if the Http status is OK.