@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
 public @ResponseBody ResponseEntity delete(@PathVariable("id") int id) {
   if (userService.getUser(id) == null) {
     return new ResponseEntity<APIMessage>(
         new APIMessage("User not exists"), HttpStatus.BAD_REQUEST);
   } else {
     userService.delete(id);
     return new ResponseEntity(HttpStatus.OK);
   }
 }
示例#2
0
 @RequestMapping(value = "")
 public ModelAndView index() {
   List<User> users = userService.getAllUsers();
   ModelAndView modelView = new ModelAndView("users/index");
   modelView.addObject("users", users);
   return modelView;
 }
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json")
  public @ResponseBody ResponseEntity update(
      @PathVariable("id") int id, HttpServletRequest request) {
    String username = request.getParameter("username");
    String city = request.getParameter("city");
    String email = request.getParameter("email");
    String telephone = request.getParameter("telephone");
    String avatar = request.getParameter("avatar");

    User user = userService.getUser(id);

    user.setUsername(username)
        .setCity(city)
        .setAvatar(avatar)
        .setEmail(email)
        .setTelephone(telephone);

    userService.update(user);
    return new ResponseEntity<APIMessage>(new APIMessage("Update success"), HttpStatus.OK);
  }
  @RequestMapping(method = RequestMethod.POST, produces = "application/json")
  public @ResponseBody ResponseEntity create(HttpServletRequest request) {
    String username = request.getParameter("username");
    String city = request.getParameter("city");
    String email = request.getParameter("email");
    String telephone = request.getParameter("telephone");
    String avatar = request.getParameter("avatar");

    if (username == null || "".equals(username)) {
      return new ResponseEntity<APIMessage>(
          new APIMessage("Username should not be null"), HttpStatus.BAD_REQUEST);
    } else {
      User user =
          new User()
              .setUsername(username)
              .setCity(city)
              .setAvatar(avatar)
              .setEmail(email)
              .setTelephone(telephone);

      userService.create(user);
      return new ResponseEntity<APIMessage>(new APIMessage("Create success"), HttpStatus.CREATED);
    }
  }
 @RequestMapping(method = RequestMethod.GET, produces = "application/json")
 public @ResponseBody ResponseEntity index() {
   List<User> users = userService.getAllUsers();
   return new ResponseEntity<List<User>>(users, HttpStatus.OK);
 }