1. Overview

In this article, we will learn spring boot etag header example, ETag header is used to reduce bandwidth and network overhead for same content which has been cached by the browser.

Let try to understand how ETag header works:

  • On the first request, server create hash code of response and set hash code as ETag in response header, server will 200 response
  • If again the same request generated by browser at that browser will send if-Non-match header which contains previous same response’s ETag value
  • When server will find if-non-match header at that time complete response’s has code with if-non-match. If both are same at that time server will send 304 response code so the browser will understand that no need to read response it’s same response which already been cache so the browser will use the cache.

In short, If the same request comes again from the same browser to server and response is same in that case server will send 304 code so the browser will not read or download complete response from the server instead it will use previously cache data. If response it very from previous response then the server will send a new response with a status code 200 so the browser will cache new response will be useful in the feature.

Etag will not improve perfomance of server but it only head to reduce bandwidth and network trafic of website.

2. Example

To implement Etag header with spring boot ShallowEtagHeaderFilter filter can be used using that we can archive our goal. Here is a complete working example:


spring boot etag header example

spring boot etag header example

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <description>spring boot ETag header example</description>
    <!-- Inherit defaults from Spring Boot -->


    <!-- Package as an executable jar -->

2.2 SpringBootConfig

We have create Bean of ShallowEtagHeaderFilter which will set Etag on response header.

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

import javax.servlet.Filter;

 * Created by JavaDeveloperZone on 19-07-2017.

@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application

    public Filter filter(){
        ShallowEtagHeaderFilter filter=new ShallowEtagHeaderFilter();
        return filter;


2.3 ETagController

package com.javadeveloperzone.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

 * Created by JavaDeveloperZone on 19-07-2017.
public class ETagController {

    public String hello() {
        return "Hello etag Header";

2.4 Output:

1st request: As we discussed above on first request, Response will be 200 and response header contains ETag which is hashcode of response, We do not worry about hashcode value it will be managed by spring boot internally.

spring boot etag header example - first request

spring boot etag header example – first request


2nd request: On second request we have the same response so the server will send 304(Not modified) status code so the browser will not download content but use content from the local browser cache.

spring boot etag header example - second request

spring boot etag header example – second request

3. Conclusion

In this example, we have seen that how to configure ETag header filter in spring boot server or application. Again I want to say that ETag will not improve the performance of server but it will help us to reduce network bandwidth.

4. References

5. Source Code

spring boot etag header example.zip (66 KB)


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

Leave a Reply

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