Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating ETAG using spring boot

I'm new to spring boot. I want to generate ETAG whenever perform a POST from my controller class.

Below is the configuration class created:

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

My main class is annotated with @EnableAutoConfiguration. As per my understanding, the response object that I receive from POST should provide me the ETAG header. Please can anyone provide a spring boot sample to generate ETag during my POST/GET?PUT call.

like image 943
user873888 Avatar asked May 20 '15 12:05

user873888


People also ask

How is an ETag generated?

Generating ETag ValueIt 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.

What is spring boot ETag?

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.

How is ETag implemented in REST API?

Step by step: we first create and retrieve a Resource – and store the ETag value for further use. then we update the same Resource. send a new GET request, this time with the “If-None-Match” header specifying the ETag that we previously stored.

What is ETag in HTTP response?

The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed.


1 Answers

The ShallowEtagHeaderFilteryou are using will only generate an ETag in response to a GET request.

When you perform a GET on the resource you just created/updated, the ETag header will be present.

It is also worth nothing that if you are making use of the Spring Repository REST exporter (i.e. Spring Data Rest), then it has ETag support built in. All that is required is for your entity classes to have a Long or Timestamp field annotated with @javax.persistence.Version

like image 87
adam p Avatar answered Oct 31 '22 13:10

adam p