I am planning to support ETag for my RESTfull Spring application. Most of the resources I expose are versioned in DB.
I am aware of ShallowEtagHeaderFilter, which is not exactly, what I need, since it only saves bandwidth.
Is there a production ready solution for Spring MVC that associates ETag header with exposed entity version?
First, you retrieve the current entity data by using a GET request that includes the If-Match request header. The ETag information is returned along with the entity content. Then, you send a PUT update request that includes the If-Match request header with the ETag information from the previous GET request.
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. It can be considered to be the more sophisticated successor to the Last-Modified header.
Generating ETag Value It can be created and updated manually or can be auto-generated. Common methods of its auto-generation include using a hash of the resource's content or just a hash of the last modification timestamp. The generated hash should be collision-free.
Approach C: caching the ETag. This is like approach B but ETag is stored in some cache middleware.
spring-data-rest supports this out-of-the-box, see the conditional request part of the reference documentation.
You can also use Spring Framework 4.2.0+ which supports conditional requests for ResponseEntity
types returned by Controller methods - see reference documentation.
Something like:
@RequestMapping("/book/{id}")
public ResponseEntity<Book> showBook(@PathVariable Long id) {
Book book = findBook(id);
String version = book.getVersion();
return ResponseEntity
.ok()
.cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
.eTag(version) // lastModified is also available
.body(book);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With