首页 > 解决方案 > 来自 RestController 的 Spring Boot 响应中没有缓存标头

问题描述

我有一个返回 JSON 的典型控制器,我需要返回no-cache标头作为响应的一部分。请注意,此时我在类路径上没有弹簧安全性,但是如果需要让它开箱即用,那么我可以接受这个

目前我已经通过以下方式实现了这一点:

public static <T> ResponseEntity<T> createResponseSpringWay(final T body) {
    org.springframework.http.CacheControl cacheControl = org.springframework.http.CacheControl.noCache();
    return ResponseEntity.ok().cacheControl(cacheControl).body(body);
}

是否有任何弹簧引导属性可以为我自动生成?我想摆脱ResponseEntity所有方法。

我尝试了以下配置,但我相信它只适用于静态资源,所以它不起作用:

spring.resources.cache.cachecontrol.no-store=true
spring.resources.cache.cachecontrol.must-revalidate=true
spring.resources.cache.cachecontrol.no-cache=true

标签: javaspringspring-bootspring-mvc

解决方案


推荐阅读