Package com.sivalabs.springapp.web.controllers

Source Code of com.sivalabs.springapp.web.controllers.UserResource

/**
*
*/
package com.sivalabs.springapp.web.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sivalabs.springapp.entities.User;
import com.sivalabs.springapp.services.UserService;

/**
* @author Siva
*
*/
@Controller
@RequestMapping("/rest/users/")
public class UserResource {

  @Autowired
  private UserService userService;
 
  @RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public List<User> findAll()
  {
    return userService.findAll();
  }
 
  @RequestMapping(value="{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public User findUser(@PathVariable("id") int id) {
    return userService.findUserById(id);
  }
 
  @RequestMapping(value="", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseEntity<User> createUser(@RequestBody User user) {
    User savedUser = userService.create(user);
    return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
  }
 
  @RequestMapping(value="", method=RequestMethod.PUT, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseEntity<User> updateUser(@RequestBody User user) {
    User savedUser = userService.update(user);
    return new ResponseEntity<User>(savedUser, HttpStatus.OK);
  }
 
  @RequestMapping(value="{id}", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseEntity<Void> deleteUser(@PathVariable("id") int id) {
    userService.deleteUser(id);
    return new ResponseEntity<>(HttpStatus.OK);
  }
 
 
 
 
}
TOP

Related Classes of com.sivalabs.springapp.web.controllers.UserResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.