使用Spring Boot 2缓存和压缩静态资源 [英] Cache and zip static resources with Spring Boot 2
本文介绍了使用Spring Boot 2缓存和压缩静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Spring Boot 2应用程序,其中的静态资源是:
I have a Spring Boot 2 application where static resources are:
src
|- main
|-resources
|-static
|-js/myjs.js
|-style
|-css/mycss.css
在我的模板文件中:
<link rel="stylesheet" type="text/css" href="/style/css/mycss.css">
<script src="/js/myjs.js"></script>
这很好.
但是我想启用浏览器缓存和gzip传输.为此,我创建了以下WebConfig:
However I want to enable browser cache and gzip transfer. To do this I have created the following 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());
}
}
该应用仍然可以运行,但是没有缓存或压缩任何静态内容:
The app still works but no static content is cached nor gzipped:
知道我在做什么错吗?
推荐答案
在Spring Boot 2.0.0中,我正在使用以下配置:
With Spring Boot 2.0.0 I'm using following config:
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
这篇关于使用Spring Boot 2缓存和压缩静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文