博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot etag header example
阅读量:2438 次
发布时间:2019-05-10

本文共 4502 字,大约阅读时间需要 15 分钟。

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

4.0.0
spring-boot-example
spring-etag-header-example
1.0-SNAPSHOT
spring boot ETag header example
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
1.8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin

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 attributepublic 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. */@RestControllerpublic 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

5. Source Code

(66 KB)

你可能感兴趣的文章
用dhtml做了一个密码管理器 (转)
查看>>
Php 3.x与4.x中关于对象编程的不兼容问题 (转)
查看>>
Cg FAQ (转)
查看>>
在access中增加农历支持模块. (转)
查看>>
增加一个判断内存变量存在的函数 (转)
查看>>
ASP文件上传神功 第二重(招势图加内功心法) (转)
查看>>
JSR227:J2EE数据绑定及数据访问标准 (转)
查看>>
Sun ONE Studio 4 Mobile Edition开发MIDlet入门 (转)
查看>>
Jbuilder8开发J2ee学习笔记(2) (转)
查看>>
Makefile编写小说(一) (转)
查看>>
NeHe的opengl教程delphi版(3)----着色 (转)
查看>>
ORACLE SQL性能优化系列 (二) (转)
查看>>
控件treeview的使用 (转)
查看>>
运用VC或Java对Office进行编程操作 (转)
查看>>
Linux Shell 裡一些很少用到卻很有用的指令 (转)
查看>>
一套日本软件开发的详细设计说明书格式(一) (转)
查看>>
回溯法解决喝酒问题 (转)
查看>>
(译)win32asm实例-7 (转)
查看>>
TTS朗读中文(using sapi sdk5) (转)
查看>>
C++面向对象特性实现机制的初步分析 Part3 (转)
查看>>