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

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)

课程简介这是一门使用Java语言,SpringBoot框架,从0开发一个RESTful API应用,接近企业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识,99%代码为手写;因为这是项目课程;所以不会深入到源码讲解某个知识点,以及原理,但会粗略的讲解下基础原理;主要是讲解如何使用系统功能,流行的第三方框架,第三方服务,完成接近企业级项目,目的是让大家,学到真正的企业级项目开发技术。适用人群刚刚毕业的学生想提高职场竞争力想学从零开发SpringBoot项目想提升SpringBoot项目开发技术想学习SpringBoot项目架构技术想学习企业级项目开发技术就是想学习SpringBoot开发能学到什么从0开发一个类似企业级项目学会能做出市面上90%通用API快速增加1到2年实际开发经验刚毕业学完后能找到满意的工作已经工作学完后最高涨薪30%课程信息全课程目前是82章,155小时,每节视频都经过精心剪辑。在线学习分辨率最高1080P课程知识点1~11章:学习方法,项目架构,编码规范,Postman使用方法,Git和Github版本控制12~16章:搭建开发环境,快速入门SpringBoot框架17~20章:快速入门MySQL数据库21~30章:MyBatis,登录注册,找回密码,发送短信,发送邮件,企业级接口配置31~41章:实现歌单,歌单标签,音乐,列表分页,视频,评论,好友功能42~48章:阿里云OSS,话题,MyBatis-plus,应用监控49~53章:Redis使用,集成Redis,SpringCache,HTTP缓存54~58章:Elasticsearch使用,集成Elasticsearch,使用ES搜索59~61章:商城,集成支付宝SDK,支付宝支付62~64章:常用哈希和加密算法,接口加密和签名65~67章:实时挤掉用户,企业级项目测试环境,企业级接口文档68~69章:SpringBoot全站HTTPS,自签证书,申请免费证书70~73章:云MySQL数据库,云Redis数据库使用,轻量级应用部署环境,域名解析74~80章:Docker使用,生产级Kubernetes集群,域名解析,集群全站HTTPS81~82章:增强和重构项目,课程总结,后续学习计划
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值