Adding Custom Headers to Spring Boot REST APIs

Learn to create HTTP POST REST APIs using Spring boot 2 framework which accept JSON request and return JSON response to client. In this Spring Boot 2 REST POST API tutorial, we will create a REST API which returns list of employees after adding a new employee to collection.

1. Maven dependencies

At first, create a simple maven web project and update following spring boot dependencies in pom.xml file.

The important dependencies are spring-boot-starter-parent (read more) and spring-boot-starter-web (read more).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


	<description>Spring Boot2 REST API Demo for</description>

		<relativePath />





2. REST Controller

In Spring, a controller class, which is capable of serving REST API requests, is called rest controller. It should be annotated with @RestController annotation. In given rest controller, we have two API methods. Feel free to add more methods as needed.


  1. It adds an employee in the employees collection.
  2. It accept employee data in Employee object.
  3. It accepts and creates JSON meda type.
  4. It accepts two HTTP headers i.e. X-COM-PERSIST and X-COM-LOCATION. First header is required and second header is optional.
  5. It returns the location of resource created.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping(path = "/employees")
public class EmployeeController 
    private EmployeeDAO employeeDao;
    @GetMapping(path="/", produces = "application/json")
    public Employees getEmployees() 
        return employeeDao.getAllEmployees();
    @PostMapping(path= "/", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> addEmployee(
        @RequestHeader(name = "X-COM-PERSIST", required = true) String headerPersist,
        @RequestHeader(name = "X-COM-LOCATION", required = false, defaultValue = "ASIA") String headerLocation,
        @RequestBody Employee employee) 
                 throws Exception 
        //Generate resource id
        Integer id = employeeDao.getAllEmployees().getEmployeeList().size() + 1;
        //add resource
        //Create resource location
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
        //Send location in response
        return ResponseEntity.created(location).build();

3. Custom error handler

A good designed resi api must have consistent error messages as well. One way to achieve it in spring boot applications is using controller advice. Inside @ControllerAdvice class, use @ExceptionHandler annotated methods to return consistent responses in invalid scenarios.


import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

public class CustomExceptionHandler
    public final ResponseEntity<Object> handleHeaderException(Exception ex, WebRequest request) 
        List<String> details = new ArrayList<>();
        ErrorResponse error = new ErrorResponse("Bad Request", details);
        return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
    public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) 
        List<String> details = new ArrayList<>();
        ErrorResponse error = new ErrorResponse("Server Error", details);
        return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);

4. @SpringBootApplication

Run the application in embedded tomcat application by executing main() method of SpringBootDemoApplication class.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 

public class SpringBootDemoApplication {

    public static void main(String[] args) {, args);

5. Model classes and DAO

These classes are not directly related to REST. Still lets take a look how they have been written.


public class Employee {

    public Employee() {


    public Employee(Integer id, String firstName, String lastName, String email) {
        super(); = id;
        this.firstName = firstName;
        this.lastName = lastName; = email;
    private Integer id;
    private String firstName;
    private String lastName;
    private String email;

    //Getters and setters

    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", 
        		lastName=" + lastName + ", email=" + email + "]";


import java.util.ArrayList;
import java.util.List;

public class Employees 
    private List<Employee> employeeList;
    public List<Employee> getEmployeeList() {
        if(employeeList == null) {
            employeeList = new ArrayList<>();
        return employeeList;
    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;

DAO class uses a static list to store data. Here we need to implement actual database interaction.


import org.springframework.stereotype.Repository;


public class EmployeeDAO 
    private static Employees list = new Employees();
        list.getEmployeeList().add(new Employee(1, "Lokesh", "Gupta", ""));
        list.getEmployeeList().add(new Employee(2, "Alex", "Kolenchiskey", ""));
        list.getEmployeeList().add(new Employee(3, "David", "Kameron", ""));
    public Employees getAllEmployees() 
        return list;
    public void addEmployee(Employee employee) {

6. Spring Boot 2 REST POST API – Demo

To start the application, run the main() method in SpringBootDemoApplication class. It will start the embedded tomcat server. In server logs, you will see that API have been registered in spring context.

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employees/],methods=[GET],produces=[application/json]}" onto public EmployeeController.getEmployees()

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employees/],methods=[POST], consumes=[application/json], produces=[application/json]}" onto public org.springframework.http.ResponseEntity <java.lang.Object> controller. EmployeeController.addEmployee( java.lang.String, java.lang.String, throws java.lang.Exception

6.1. HTTP POST – Validate missing header

Once server is UP, access the API using some rest client. Do not pass the request headers.

Spring boot rest post - missing header error
Spring boot rest post – missing header error
"message": "Bad Request",
	"details": [
		"Missing request header 'X-COM-PERSIST' for method parameter of type String"

6.2. HTTP POST – Valid response

Valid response
Valid response – resource created
location: http://localhost:8080/employees/4
content-length: 0
date: Sat, 06 Oct 2018 04:33:37 GMT

Hit the GET request and this time we will get the added employee as well.

Spring Boot REST HTTP GET - Updated
Spring Boot REST HTTP GET – Updated

Let me know if you have query in this spring boot post request example.

Happy Learning !!


Spring boot starters

Leave a Reply

Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial