spring boot etag header example

本文介绍如何使用Spring Boot配置ETag头,通过ShallowEtagHeaderFilter减少带宽消耗和网络流量,提升网站性能。ETag不会直接提高服务器性能,但能有效减少重复内容的传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自

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

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值