使用 Spring Boot 2 缓存和压缩静态资源

Vmx*_*xes 1 spring caching spring-boot

我有一个 Spring Boot 2 应用程序,其中静态资源是:

src 
|-  main
    |-resources 
        |-static
            |-js/myjs.js
            |-style
                |-css/mycss.css
Run Code Online (Sandbox Code Playgroud)

在我的模板文件中:

<link rel="stylesheet" type="text/css" href="/style/css/mycss.css">
<script src="/js/myjs.js"></script>
Run Code Online (Sandbox Code Playgroud)

这工作正常。

不过我想启用浏览器缓存和 gzip 传输。为此,我创建了以下 WebConfig:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("/static/")
                .setCachePeriod(3600)
                .resourceChain(true)
                .addResolver(new GzipResourceResolver())
                .addResolver(new PathResourceResolver());
    }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序仍然可以运行,但不会缓存或压缩静态内容:

在此输入图像描述

知道我做错了什么吗?

Mam*_*dar 5

对于 Spring Boot 2.0.0,我使用以下配置:

server.compression.enabled=true
spring.resources.cache.cachecontrol.cache-public=true
spring.resources.cache.cachecontrol.no-cache=false
spring.resources.cache.cachecontrol.no-store=false
spring.resources.cache.cachecontrol.must-revalidate=false
spring.resources.cache.cachecontrol.max-age=31536000
Run Code Online (Sandbox Code Playgroud)