1. Overview

We will explore here how to add custom header in spring boot application or Spring boot add custom response headers example. Here we explain three methods to add custom header in the response.

Using Filter we can add custom headers to all response and while using ResponseEntity or HttpServletResponse we can write headers to the particular or individual response.

NOTE : All three ways not only limited to spring boot, It part of Spring web MVC so it will also work with Spring MVC application. For our explaination and example we are using spring boot framework.

2. Example

2.1 Add custom header using Filter

Register filter using @Component so spring framework flow comes here for every response. Here we have added Headers using HttpServletResponse.setHeader() method. Do not forget to add  chain.doFilter() at last to continue other execution of filter chain.

package com.javadeveloperzone;


import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by JavaDeveloperZone on 16-12-2017.
 */
@Component
public class CustomHeaderFilter implements Filter {

    @Override
    public void destroy() {
        System.out.println("destroy filter. release our resources here if any");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
                                                                                              IOException,ServletException {
        HttpServletResponse httpServletResponse=(HttpServletResponse)response;
        httpServletResponse.setHeader("Custom-Filter-Header","Write Header using Filter");
        chain.doFilter(request, response);      // continue execution of other filter chain.
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Init filter");
    }

}

2.2 Add custom header using ResponseEntity

To add Header to the particular response. ResponseEntity only work will rest service response.

@GetMapping(value = "/hello-entity-headers")
   public ResponseEntity helloEntityHeaders(){
       HttpHeaders responseHeaders = new HttpHeaders();
       responseHeaders.set("Custom-Entity-Header-","Write Header Using ResponseEntity");
       ResponseEntity responseEntity = new ResponseEntity("Spring boot Custom header Example",responseHeaders,HttpStatus.OK);
       return responseEntity;
   }

2.3 Add custom header using HttpServletResponse

In Spring-MVC application, when we are returning View name at that time we can use HttpServletResponse to write header to add Header in particular response.

    @GetMapping(value = "/hello-response-headers")
    public String helloResponseHeaders(HttpServletResponse response){
        response.addHeader("Custom-Response-Headers","Write header using HttpServletResponse in Controllers");
        return "Spring boot Custom header Example";
    }

Output:

Spring boot add custom response headers - output

Spring boot add custom response headers – output

3. References

4. Source Code

spring-boot-set-custom-headers-example (176 KB)

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *