Updating a Todo

We will now add the method for updating the details of an existing Todo. The HTTP method to be used for updating a resource is PUT. We will post to a "/users/{name}/todos/{id}" URI:

@PutMapping("/users/{name}/todos/{id}")
public ResponseEntity<Todo> updateTodo(@PathVariable String name, @PathVariable int id,
@RequestBody Todo todo) {

todoService.update(todo);

return new ResponseEntity<Todo>(todo, HttpStatus.OK);

}

A few things to note are as follows:

  • @PutMapping("/users/{name}/todos/{id}"): @PutMapping annotations map the update() method to the HTTP request with a PUT method.
  • public ResponseEntity<Todo> updateTodo(@PathVariable String name, @PathVariable int id, @RequestBody Todo todo): An HTTP PUT request should ideally return the content of the updated resource. We use ResourceEntity to do this. @RequestBody binds the body of the request directly to the bean.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset