when to use responseentity in spring boot code example
Example 1: sending status code along with entity in spring boot
package com.example.demo.com.example.demo.resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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.example.demo.com.example.demo.model.Employee;
import com.example.demo.com.example.demo.repository.EmployeeRepo;
@RestController
@RequestMapping(value = "/webapi")
public class EmployeeResource {
@Autowired
private EmployeeRepo employeeRepo;
@RequestMapping(method = RequestMethod.GET, value = "/employees")
public List<Employee> getAllEmployees() {
return employeeRepo.findAll();
}
@RequestMapping(method = RequestMethod.POST, value = "/addemployee")
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
employeeRepo.save(employee);
System.out.println(employee);
return new ResponseEntity<Employee>(employee, HttpStatus.CREATED);
}
@RequestMapping(method = RequestMethod.GET, value = "/employees/{employeeId}")
public ResponseEntity<Object> getEmployee(@PathVariable String employeeId) {
Optional<Employee> employee = employeeRepo.findById(employeeId);
if (employeeRepo.findById(employeeId).isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(employee);
} else {
return ResponseEntity.status(HttpStatus.OK).body(employee);
}
}
@RequestMapping(method = RequestMethod.DELETE, value = "/deleteemployee/{employeeId}")
public String deleteEmployee(@PathVariable String employeeId) {
employeeRepo.deleteById(employeeId);
return "Record Deleted : " + employeeId;
}
@RequestMapping(method = RequestMethod.PUT, value = "/updateemployee")
public ResponseEntity<Object> updateemployee(@RequestBody Employee employee) {
if (employeeRepo.existsById(employee.getEmployeeId())) {
employeeRepo.save(employee);
return ResponseEntity.status(HttpStatus.ACCEPTED).body(employee);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(employee);
}
}
}
Example 2: responseentity object
package com.zetcode.controller;
import com.zetcode.bean.Country;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping(value = "/getCountry")
public ResponseEntity<Country> getCountry() {
var c = new Country();
c.setName("France");
c.setPopulation(66984000);
var headers = new HttpHeaders();
headers.add("Responded", "MyController");
return ResponseEntity.accepted().headers(headers).body(c);
}
@RequestMapping(value = "/getCountry2")
@ResponseBody
public Country getCountry2() {
var c = new Country();
c.setName("France");
c.setPopulation(66984000);
return c;
}
}