// curl -X PUT -i http://localhost:8080/todo-app/api/todo/{id} -d // '{"text":"TODO Item Text","done":true}' @RequestMapping(value = "todo/{id}", method = RequestMethod.PUT) @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@PathVariable String id, @RequestBody Todo todo) { Todo existing = todoService.get(id); if (existing == null) throw new NotFoundException(); existing.setText(todo.getText()); existing.setDone(todo.isDone()); }
// curl -X GET -i http://localhost:8080/todo-app/api/todo/{id} @RequestMapping(value = "todo/{id}", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) @ResponseBody public Todo todo(@PathVariable String id) { Todo todo = todoService.get(id); if (todo == null) throw new NotFoundException(); return todo; }
// curl -X POST -i http://localhost:8080/todo-app/api/todo?text=TodoItem1 @RequestMapping(value = "todo", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) @ResponseBody public void create(@RequestParam String text, HttpServletRequest req, HttpServletResponse resp) { Todo todo = todoService.createNewTodo(text); StringBuffer url = req.getRequestURL().append("/{id}"); UriTemplate uriTemplate = new UriTemplate(url.toString()); resp.addHeader("location", uriTemplate.expand(todo.getId()).toASCIIString()); }
// curl -X DELETE -i http://localhost:8080/todo-app/api/todo/{id} @RequestMapping(value = "todo/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) public void delete(@PathVariable String id) { todoService.delete(id); }
// curl -X GET -i http://localhost:8080/todo-app/api/todo @RequestMapping(value = "todo", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) @ResponseBody public Todos todos() { return new Todos(todoService.getAllTodos()); }