Using ETag In RESTful Services – Spring Boot

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

The Spring framework provides an ETag filter with the name ShallowEtagHeaderFilter, that we can use to enable ETag headers in the response.

This ETag filter generates a hash for every response. The spring framework adds this hash to the response header.

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

Adding ETag filter in spring boot

Create a spring configuration class with the name ETagConfig as shown below. Also, annotate the class with the @EnableWebMvc and the @Configuration annotations.

@EnableWebMvc
@Configuration
public class EtagConfig {
    
    @Bean
    public Filter etagFilter() {
        return new ShallowEtagHeaderFilter();
    }
}

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

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

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class EmployeeDTO {
    
    private Integer id;
    private String empName;
}

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

@RestController
public class EmployeeController {

    @GetMapping("/get-json")
    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 the 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, the spring boot service returns the 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.

Conclusion

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.

Using ETag In RESTful Services – Spring Boot
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading