首页 > 其他分享 >spring 服务端如何设置 Last-Modified If-Modified-Since

spring 服务端如何设置 Last-Modified If-Modified-Since

时间:2024-07-18 16:09:11浏览次数:6  
标签:Last spring Since Modified lastModified Date import

在Spring框架中,设置HTTP响应头Last-Modified和处理请求头If-Modified-Since是优化缓存和减少不必要数据传输的常用策略。Spring MVC提供了灵活的机制来实现这一点。

设置Last-Modified响应头

要在Spring MVC中设置Last-Modified响应头,你可以在你的Controller方法中返回一个ResponseEntity对象,或者在方法上添加@LastModifiedDate注解(如果你使用的是Spring Data JPA等库),或者在你的方法体内部直接操作HttpServletResponse

示例1:使用ResponseEntity

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

import java.util.Date;

@RestController
public class MyController {

    @GetMapping("/resource")
    public ResponseEntity<String> getResource() {
        // 假设这是你的资源最后修改时间
        Date lastModified = new Date(); // 这里应该是从数据源获取的

        // 创建ResponseEntity并设置状态码、body和headers
        return ResponseEntity.ok()
                .lastModified(lastModified) // 设置Last-Modified
                .body("Resource content");
    }
}

示例2:使用HttpServletResponse

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;

@RestController
public class MyController {

    @GetMapping("/resource")
    public void getResource(HttpServletResponse response) throws IOException {
        // 假设这是你的资源最后修改时间
        Date lastModified = new Date(); // 这里应该是从数据源获取的

        // 设置Last-Modified响应头
        response.setDateHeader("Last-Modified", lastModified.getTime());

        // 设置响应体内容(这里示例为文本)
        response.getWriter().write("Resource content");
    }
}

处理If-Modified-Since请求头

当客户端发送一个包含If-Modified-Since请求头的请求时,你需要在你的Controller中读取这个请求头,并比较它与资源的最后修改时间。如果资源自该时间以来未被修改,则返回一个304 Not Modified状态码。

示例

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;

@RestController
public class MyController {

    @GetMapping("/resource")
    public void getResource(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 假设这是你的资源最后修改时间
        Date lastModified = new Date(System.currentTimeMillis() - 10000); // 示例:10秒前

        // 获取If-Modified-Since请求头
        long ifModifiedSince = request.getDateHeader("If-Modified-Since");

        // 将If-Modified-Since请求头转换为Date对象
        Date clientLastModified = new Date(ifModifiedSince);

        // 比较两个时间
        if (lastModified.before(clientLastModified) || lastModified.equals(clientLastModified)) {
            // 资源自If-Modified-Since指定的时间以来未修改,返回304 Not Modified
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        } else {
            // 资源已修改,正常返回资源内容
            response.setDateHeader("Last-Modified", lastModified.getTime());
            response.getWriter().write("Resource content (modified)");
        }
    }
}

注意,这个示例中我简化了资源内容的获取和返回,实际使用中你可能需要从数据库中查询资源的最后修改时间,并返回相应的资源内容。

标签:Last,spring,Since,Modified,lastModified,Date,import
From: https://www.cnblogs.com/sunupo/p/18309759

相关文章

  • 基于springboot的小区物业管理系统
    全文内容包括:1、采用技术;2、系统功能;3、系统截图;4、配套内容。索取方式见文末微信号,欢迎关注收藏!一、采用技术语言:Java1.8框架:SpringBoot数据库:MySQL5.7、8.0开发工具:IntelliJIDEA旗舰版其他:Maven3.8以上二、系统功能物业人员管理:负责物业员工的信息录入、编辑、权限分......
  • 基于SpringBoot的宠物领养系统-07863(免费领源码+开发文档)可做计算机毕业设计JAVA、PHP
    摘 要21世纪的今天,随着社会的不断发展与进步,人们对于信息科学化的认识,已由低层次向高层次发展,由原来的感性认识向理性认识提高,管理工作的重要性已逐渐被人们所认识,科学化的管理,使信息存储达到准确、快速、完善,并能提高工作管理效率,促进其发展。论文主要是对宠物领养系统......
  • spring 服务端如何设置 If-None-Match 和 ETAG
    在Spring框架中,特别是使用SpringMVC或SpringBoot时,设置ETag和处理If-None-Match请求头通常是通过一些自定义的逻辑来实现的,因为SpringMVC本身不直接提供自动化的ETag生成和验证机制。不过,你可以通过以下几种方式来实现:1.使用拦截器(Interceptor)或过滤器(Filter)你可以创建一个......
  • SpringBoot增加管理后台登录拦截器验证用户登录
    一、增加拦截器@ComponentpublicclassAdminLoginInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecto)throwsException{StringrequestServletPath=request.getS......
  • 使用Spring Boot和OpenAPI构建RESTful API文档化系统
    使用SpringBoot和OpenAPI构建RESTfulAPI文档化系统大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在当今的软件开发中,构建RESTfulAPI是非常常见的任务。随着微服务架构的流行,API的文档化和管理变得尤为重要。本文将介绍如何利用SpringBoot和OpenAPI(S......
  • springboot整合mqtt
    安装emqxhttps://blog.csdn.net/weixin_41542513/article/details/134328627springboot整合mqtt1、引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration&l......
  • SSM 整合(Spring + MyBatis;Spring + Spring MVC)
    1.SSM整合(Spring+MyBatis;Spring+SpringMVC)文章目录1.SSM整合(Spring+MyBatis;Spring+SpringMVC)2.引入相关依赖3.SSM整合3.1创建包结构4.Spring整合+MyBatis4.1编写jdbc.properties4.2编写DataSourceConfig数据源配置4.3编写MyBatisConf......
  • 实现基于Spring Boot和WebSockets的实时通讯应用
    实现基于SpringBoot和WebSockets的实时通讯应用大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!实时通讯应用在现代互联网服务中越来越重要。使用WebSocket可以实现客户端和服务器之间的双向通讯,从而大大提高实时性。本文将介绍如何使用SpringBoot和Web......
  • springboot博客交流平台-计算机毕业设计源码56406
    摘要博客交流平台作为一种重要的网络平台,为用户提供了展示自我、分享经验和与他人互动的空间。在国内外,研究者们关注博客交流平台的各个方面,并取得了显著的进展。研究内容主要包括用户体验和界面设计、社交化和互动性、多媒体内容支持、移动设备适配和跨平台体验、数据分析......
  • springboot访问多个mysql数据库配置多数据源
    一、参考地址:https://github.com/baomidou/dynamic-datasource二、使用方法引入dynamic-datasource-spring-boot-starter或者dynamic-datasource-spring-boot3-starter。spring-boot1.5.x2.x.x点击查看代码<dependency><groupId>com.baomidou</groupId><art......