About Eugen Paraschiv

ETags for REST with Spring

1. Overview

This article will focus on ETags – the Spring support, integration testing of the RESTful API, and consumption scenarios with curl. This is the ninth of a series of articles about setting up a secure RESTful Web Service using Spring 3.1 and Spring Security 3.1 with Java based configuration.

2. REST and ETags

From the official Spring documentation on ETag support:

An ETag (entity tag) is an HTTP response header returned by an HTTP/1.1 compliant web server used to determine change in content at a given URL.

ETags are used for two things – caching and conditional requests. The ETag value can be though as a hash computed out of the bytes of the Response body. Because a cryptographic hash function is likely used, even the smallest modification of the body will drastically change the output and thus the value of the ETag. This is only true for strong ETags – the protocol does provide a weak Etag as well.

Using an If-* header turns a standard GET request into a conditional GET. The two If-* headers that are using with ETags are “If-None-Match” and “If-Match” – each with it’s own semantics as discussed later in this article.

3. Client-Server communication with curl

A simple Client-Server communication involving ETags can be broken down into the steps:

– first, the Client makes a REST API call – the Response includes the ETag header to be stored for further use:

– next request the Client makes to the RESTful API includes the If-None-Match request header with the ETag value from the previous step; if the Resource has not changed on the Server, the Response will contain no body and a status code of 304 – Not Modified:

– finally, we send out the the last request to retrieve the Privilege again; keep in mind that it has been updated since the last time it was retrieved, so the previous ETag value should no longer work – the response will contain the new data and a new ETag which, again, can be stored for further use:

The filter is mapped on the same URI pattern as the RESTful API itself. The filter itself is the standard implementation of ETag functionality since Spring 3.0.

The implementation is a shallow one – the ETag is calculated based on the response, which will save bandwidth but not server performance. So, a request that will benefit from the ETag support will still be processed as a standard request, consume any resource that it would normally consume (database connections, etc) and only before having it’s response returned back to the client will the ETag support kick in.

At that point the ETag will be calculated out of the Response body and set on the Resource itself; also, if the If-None-Match header was set on the Request, it will be handled as well.

A deeper implementation of the ETag mechanism could potentially provide much greater benefits – such as serving some requests from the cache and not having to perform the computation at all – but the implementation would most definitly not be as simple, nor as pluggable as the shallow approach described here.

5. Testing ETags

Let’s start simple – we need to verify that the response of a simple request retrieving a single Resource will actually return the “ETag” header:

a Resource is first created and then retrieved – the ETag value is stored for further use

the same Resource is then updated

a new retrieve request is sent, this time with the “If-None-Match” header specifying the ETag value previously stored

on this second request, the server will returns a 200 OK along with the full Resource, since the ETag value is no longer correct, as the Resource has been updated in the meantime

Next, we test the behaviour for “If-Match” – the ShallowEtagHeaderFilter does not have out of the box support for the If-Match HTTP header (being tracked on this JIRA issue), so the following test should fail:

7. Conclusion

This article only scratched the surface with what’s possible with Spring and ETags. For a full implementation of an ETag enabled RESTful service, along with integration tests verifying the ETag behaviour, check out the github project.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Recent Jobs

No job listings found.

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.