相关疑难解决方法(0)

如何在Spring Boot中启用HTTP响应缓存

我使用Spring Boot 1.0.2实现了一个REST服务器.我无法阻止Spring设置禁用HTTP缓存的HTTP标头.

我的控制器如下:

@Controller
public class MyRestController {
    @RequestMapping(value = "/someUrl", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<String> myMethod(
            HttpServletResponse httpResponse) throws SQLException {
        return new ResponseEntity<String>("{}", HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

所有HTTP响应都包含以下标头:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
Run Code Online (Sandbox Code Playgroud)

我已尝试以下方法删除或更改这些标头:

  1. 呼叫setCacheSeconds(-1)控制器.
  2. 呼叫httpResponse.setHeader("Cache-Control", "max-age=123")控制器.
  3. 定义我调用的@Bean返回值.WebContentInterceptorsetCacheSeconds(-1)
  4. 将属性设置spring.resources.cache-period为-1或正值application.properties.

以上都没有任何影响.如何在Spring Boot中为所有或单个请求禁用或更改这些标头?

java spring spring-mvc spring-security spring-boot

47
推荐指数
4
解决办法
6万
查看次数

如何在SpringBoot中将缓存控制设置为CSS和JS文件?

我是SpringBoot的新手。

我基于Springboot的示例项目来创建我的项目。我想仅针对js / css文件控制用于缓存的http标头。

我在下面添加了一个js文件test.js,src/resources/static然后在中引用它greeting.html。

然后,我在StackOverflow上遵循一些答案,并添加WebMvcConfiguration.java和WebSecurityConfig.java,但它对我不起作用。

我确定WebMvcConfiguration和WebSecurityConfig不应一起使用,或者我配置有误。我更喜欢使用Java Config而不是XML的解决方案。

我打开ChromeDevTool来检查服务器的响应。响应http标头是

Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 10:53:14 GMT
Expires:
Last-Modified:Thu, 02 Feb 2017 10:52:49 GMT
Pragma:
Run Code Online (Sandbox Code Playgroud)

谢谢!

更新:当我删除时WebSecurityConfig,我得到以下标题

Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 11:14:51 GMT
Last-Modified:Thu, 02 Feb 2017 11:14:20 GMT
Run Code Online (Sandbox Code Playgroud)

greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script th:src="@{/test.js}"></script>
</head> …
Run Code Online (Sandbox Code Playgroud)

cache-control spring-security spring-boot

5
推荐指数
1
解决办法
4592
查看次数