ETags can be used as caching while developing RESTful web services with spring boot. Spring framework provides support for ETags by providing the request filters.

Spring framework provides an ETag filter called ShallowEtagHeaderFilter, that can be used to enable ETag headers in the response.

This ETag filter generates a hash for every response. This hash is added to the response header and sent back to the client.

Service clients can use this ETag header value and send an If-None-Match request header, whenever they are requesting for the same resource nest time.

Adding ETag filter in spring boot

Create a configuration class called as shown below. annotate the class with @EnableWebMvc and @Configuration.

public class EtagConfig {
	public Filter etagFilter() {
		return new ShallowEtagHeaderFilter();

In the above configuration class, we are creating an HTTP Header filter bean called ShallowEtagHeaderFilter. This filter adds the ETag response header with the calculated hash value for the response object.

Create an DTO class as shown below. This is a simple POJO class, with two fields id and empName.

public class EmployeeDTO {
	private Integer id;
	private String empName;

Create a RESTful controller class called as shown below. This controller class contains a simple HTTP GET request mapping, mapped to “/get-json” endpoint. Upon invoking the endpoint, we get a new EmployeeDTO object.

public class EmployeeController {

	public ResponseEntity<EmployeeDTO> getObject() {
		EmployeeDTO dto = new EmployeeDTO(1, "Arun");
		return new ResponseEntity<>(dto, HttpStatus.OK);

Start the spring boot application. If we invoke the service endpoint for the first time using curl command, we should be able to get the response JSON as shown below.

The response contains the ETag response header as displayed in the below image.

etag example spring rest

Once the ETag is received from the service, consecutive requests for the resource can be made along with the If-None-Match request header with ETag hash as a header value.

The below image shows the curl HTTP GET request along with the If-None-Match request header.

etag example spring boot.

Upon receiving the request, spring boot service returns HTTP status code 304(Not Modified), if there are no changes in the response value. Service will not send any response body, as there is no change in the response.


In this article, we learned how to enable ETag headers in spring boot RESTful web service applications.

We also learned how to use the If-None-Match request header to check if there is any change in the retrieved response or not.

You may also interested in