0
点赞
收藏
分享

微信扫一扫

axios的使用,处理请求和响应,axios拦截器

心存浪漫 2024-07-24 阅读 27

ETag简介

在Web开发中,ETag(Entity Tag)是一种HTTP头字段,用于标识特定版本的资源。ETag的主要用途是缓存控制和优化,通过比较客户端和服务器资源的ETag值,可以判断资源是否发生变化,从而避免不必要的数据传输。ETag通常是一个字符串,可以是一个哈希值、版本号或者时间戳。

Springboot接口添加ETag

可以通过Spring Boot提供通的ShallowEtagHeaderFilter实现简单的ETag接口。有时候我们需要自己手动设置ETag。需要注意的是,ETag头的值必须符合HTTP规范。通常,ETag值应该用双引号括起来,例如 "123456"。示例如下

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/example")
    public ResponseEntity<String> getExample() {
        String version = "123456";
        return ResponseEntity.ok()
                .header(HttpHeaders.ETAG, "\"" + version + "\"")
                .body("Example response");
    }
}

测试ETag

使用curl命令的-I参数显示HTTP响应的消息头

curl -I http://localhost:8080/example

举报

相关推荐

0 条评论