Last active
December 21, 2016 14:32
-
-
Save bytestree/2da8891c01aaa8cf7f2bb409cccdea54 to your computer and use it in GitHub Desktop.
Restful Web Service Controller for CRUD operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytestree.restful.controller; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.apache.log4j.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpStatus; | |
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 com.bytestree.restful.model.Employee; | |
import com.bytestree.restful.service.EmployeeService; | |
@RestController | |
@RequestMapping("/employee") | |
public class EmployeeController { | |
final static Logger logger = Logger.getLogger(EmployeeController.class); | |
@Autowired | |
EmployeeService empService; | |
@RequestMapping(method = RequestMethod.POST) | |
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) { | |
empService.save(employee); | |
logger.debug("Added:: " + employee); | |
return new ResponseEntity<Employee>(employee, HttpStatus.CREATED); | |
} | |
@RequestMapping(method = RequestMethod.PUT) | |
public ResponseEntity<Void> updateEmployee(@RequestBody Employee employee) { | |
Employee existingEmp = empService.getById(employee.getId()); | |
if (existingEmp == null) { | |
logger.debug("Employee with id " + employee.getId() + " does not exists"); | |
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); | |
} else { | |
empService.save(employee); | |
return new ResponseEntity<Void>(HttpStatus.OK); | |
} | |
} | |
@RequestMapping(value = "/{id}", method = RequestMethod.GET) | |
public ResponseEntity<Employee> getEmployee(@PathVariable("id") Long id) { | |
Employee employee = empService.getById(id); | |
if (employee == null) { | |
logger.debug("Employee with id " + id + " does not exists"); | |
return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND); | |
} | |
logger.debug("Found Employee:: " + employee); | |
return new ResponseEntity<Employee>(employee, HttpStatus.OK); | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
public ResponseEntity<List<Employee>> getAllEmployees() { | |
List<Employee> employees = empService.getAll(); | |
if (employees.isEmpty()) { | |
logger.debug("Employees does not exists"); | |
return new ResponseEntity<List<Employee>>(HttpStatus.NO_CONTENT); | |
} | |
logger.debug("Found " + employees.size() + " Employees"); | |
logger.debug(employees); | |
logger.debug(Arrays.toString(employees.toArray())); | |
return new ResponseEntity<List<Employee>>(employees, HttpStatus.OK); | |
} | |
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) | |
public ResponseEntity<Void> deleteEmployee(@PathVariable("id") Long id) { | |
Employee employee = empService.getById(id); | |
if (employee == null) { | |
logger.debug("Employee with id " + id + " does not exists"); | |
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); | |
} else { | |
empService.delete(id); | |
logger.debug("Employee with id " + id + " deleted"); | |
return new ResponseEntity<Void>(HttpStatus.GONE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks