responseentity with status code and message 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 error message

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity getUser(@RequestHeader(value="Access-key") String accessKey,
                              @RequestHeader(value="Secret-key") String secretKey) {
    try {
        // see note 1
        return ResponseEntity
            .status(HttpStatus.CREATED)                 
            .body(this.userService.chkCredentials(accessKey, secretKey, timestamp));
    }
    catch(ChekingCredentialsFailedException e) {
        e.printStackTrace(); // see note 2
        return ResponseEntity
            .status(HttpStatus.FORBIDDEN)
            .body("Error Message");
    }
}

Tags:

Java Example