In my previous post, I explained simple REST
application. You can download previous working application from this link.
Areas of improvement
a.
We are not done any validations while performing CRUD
operations on Employee. For example, we should return not found response code
when employee not exist, we should return internal server error in exceptional
cases and we should return status code 201 for new object creation.
How can we improve previous application?
We can user ResponseEntity object, to send the response
properly. ResponseEntity class is used to set the response body, status code
and headers.
For example, below snippet return collection of
employee as ResponseEntity.
@RequestMapping(value = "employees", method
= RequestMethod.GET)
public ResponseEntity<Collection<Employee>>
all() {
return
ResponseEntity.ok(EmployeeUtil.all());
}
You can even set different status code for different
scenarios. For example, below snippet return 404 status code if employee not
found.
@RequestMapping(value = "employees/{id}",
method = RequestMethod.GET)
public ResponseEntity<Employee>
byId(@PathVariable int id) {
Employee emp
= EmployeeUtil.byId(id);
if (emp ==
null) {
return
new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return
ResponseEntity.ok(emp);
}
You can even set response header using below
constructor.
ResponseEntity(@Nullable T body, @Nullable
MultiValueMap<String, String> headers, HttpStatus status)
Use ok method to send the response payload with status
code 200.
ResponseEntity.ok(emp);
Update EmployeeController of previous application like
below.
EmployeeController.java
package com.sample.app.controller; import java.util.Collection; 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.sample.app.model.Employee; import com.sample.app.util.EmployeeUtil; @RestController @RequestMapping("api/v1/") public class EmployeeController { @RequestMapping(value = "employees", method = RequestMethod.GET) public ResponseEntity<Collection<Employee>> all() { return ResponseEntity.ok(EmployeeUtil.all()); } @RequestMapping(value = "employees", method = RequestMethod.POST) public ResponseEntity<Employee> create(@RequestBody Employee emp) { return new ResponseEntity<>(EmployeeUtil.create(emp), HttpStatus.CREATED); } @RequestMapping(value = "employees/{id}", method = RequestMethod.GET) public ResponseEntity<Employee> byId(@PathVariable int id) { Employee emp = EmployeeUtil.byId(id); if (emp == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(emp); } @RequestMapping(value = "employees/{id}", method = RequestMethod.DELETE) public ResponseEntity<Employee> deleteById(@PathVariable int id) { Employee emp = EmployeeUtil.byId(id); if (emp == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(EmployeeUtil.delete(id)); } @RequestMapping(value = "employees/{id}", method = RequestMethod.PUT) public ResponseEntity<Employee> updateById(@PathVariable int id, @RequestBody Employee emp) { Employee currentEmp = EmployeeUtil.byId(id); if (currentEmp == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(EmployeeUtil.updateById(id, emp)); } }
Run App.java.
Request for non-exist employee
As you see above image, when I request for non-exist
employee, I got response status code as 404.
Create new employee
As you see above image, when I create new employee, I
got response status code as 201.
You can download complete working example from this link.
No comments:
Post a Comment