How to add support for Shallow ETag in ResponseEntity Spring Java

676 Views Asked by At

I understand that we can create the filter for Shallow E tag in spring. However, i am looking if there is an easier way to do it. I am using HttpEntity in my code for getting the response. The code looks like this

return ResponseEntity.ok()
                    .eTag(getETag(preloadMap))
                    .body(preloadMap);

However, I am looking forward to options of changing this etag to Shallow etag in a simpler way.

1

There are 1 best solutions below

1
Daniel Domingueti On

I think using spring filter is the easiest way to implement shallow etag. Within three lines it's done.

@Configuration
public MyConfigClass () {
    @Bean
    public Filter shallowEtagHeaderFilter() {
        return new ShallowEtagHeaderFilter();
    }
}

This filter is in charge of generating a hash response and adding it up to the etag header.

It also verifies if the created hash matches with the existing etag from "if-none-match". If both are equal it returns 304 Not modified. Otherwise it just returns 200 OK.