Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.springrest.controllers; import com.mycompany.springrest.model.User; import com.mycompany.springrest.services.UserService; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; 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.RestController; import org.springframework.web.util.UriComponentsBuilder; /** * * @author ?? */ @RestController @RequestMapping(value = "/userservice") public class UserController { private static final Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired(required = true) private UserService userService; @RequestMapping(value = "/user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<List<User>> listUsers() { List<User> users = userService.listUsers(); if (users.isEmpty()) { return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT); } return new ResponseEntity<List<User>>(users, HttpStatus.OK); } @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> getUser(@PathVariable("id") int id) { User user = userService.getUserById(id); logger.info("Fetching User with id " + id); if (user == null) { logger.info("User with id " + id + " not found"); return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } return new ResponseEntity<User>(user, HttpStatus.OK); } @RequestMapping(value = "/user/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) { logger.info("Creating User " + user.getUserName()); if (userService.isUserExist(user)) { logger.info("A User with name " + user.getUserName() + " already exist"); return new ResponseEntity<Void>(HttpStatus.CONFLICT); } userService.addUser(user); HttpHeaders headers = new HttpHeaders(); headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri()); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); } @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> updateUser(@PathVariable("id") int id, @RequestBody User user) { logger.info("Updating User " + id); User currentUser = userService.getUserById(id); if (currentUser == null) { logger.info("User with id " + id + " not found"); return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } currentUser.setUserName(user.getUserName()); currentUser.setPassWord(user.getPassWord()); currentUser.setRoles(user.getRoles()); currentUser.setIsActive(user.isIsActive()); userService.updateUser(currentUser); return new ResponseEntity<User>(currentUser, HttpStatus.OK); } @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> deleteUser(@PathVariable("id") int id) { logger.info("Fetching & Deleting User with id " + id); User user = userService.getUserById(id); if (user == null) { logger.info("Unable to delete. User with id " + id + " not found"); return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } userService.removeUser(id); return new ResponseEntity<User>(HttpStatus.NO_CONTENT); } }