spring boot etag header example

转自

1. Overview

In this article, we will learn spring boot etag header example, ETag header is used to reduce bandwidth and network overhead for same content which has been cached by the browser.

Let try to understand how ETag header works:

On the first request, server create hash code of response and set hash code as ETag in response header, server will 200 response
If again the same request generated by browser at that browser will send if-Non-match header which contains previous same response’s ETag value
When server will find if-non-match header at that time complete response’s has code with if-non-match. If both are same at that time server will send 304 response code so the browser will understand that no need to read response it’s same response which already been cache so the browser will use the cache.
In short, If the same request comes again from the same browser to server and response is same in that case server will send 304 code so the browser will not read or download complete response from the server instead it will use previously cache data. If response it very from previous response then the server will send a new response with a status code 200 so the browser will cache new response will be useful in the feature.

Etag will not improve perfomance of server but it only head to reduce bandwidth and network trafic of website.

2. Example

To implement Etag header with spring boot ShallowEtagHeaderFilter filter can be used using that we can archive our goal. Here is a complete working example:

spring boot etag header example
spring boot etag header example

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-boot-example</groupId>
    <artifactId>spring-etag-header-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>spring boot ETag header example</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 SpringBootConfig

We have create Bean of ShallowEtagHeaderFilter which will set Etag on response header.

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.Filter;
/**
* Created by JavaDeveloperZone on 19-07-2017.
*/
@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
   public static void main(String[] args) throws Exception {
       SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
   }
   @Bean
   public Filter filter(){
       ShallowEtagHeaderFilter filter=new ShallowEtagHeaderFilter();
       return filter;
   }
}

2.3 ETagController

package com.javadeveloperzone.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@RestController
public class ETagController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello etag Header";
    }
}

2.4 Output:
1st request: As we discussed above on first request, Response will be 200 and response header contains ETag which is hashcode of response, We do not worry about hashcode value it will be managed by spring boot internally.

spring boot etag header example – first request

扫描二维码关注公众号,回复: 3581125 查看本文章

2nd request: On second request we have the same response so the server will send 304(Not modified) status code so the browser will not download content but use content from the local browser cache.

spring boot etag header example - second request

3. Conclusion

In this example, we have seen that how to configure ETag header filter in spring boot server or application. Again I want to say that ETag will not improve the performance of server but it will help us to reduce network bandwidth.

4. References

ShallowEtagHeaderFilter API Document

5. Source Code

spring boot etag header example.zip (66 KB)

猜你喜欢

转载自blog.csdn.net/cnhome/article/details/82855815