在spring boot中为静态资源设置setCachePeriod [英] Set setCachePeriod for static resources in spring boot
问题描述
我正在使用spring boot,并且 / static
作为静态资源(如js和css)提供,到目前为止一直很好,而我想设置缓存头这些文件,所以我试过这个:
I am using spring boot, and the /static
is served as static resources like js and css, so far so good, while I want to set the cache header of these files, so I tried this:
@Configuration
public class BaseMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").setCachePeriod(24 * 3600 * 365);
}
}
然而,之后,该应用程序无法提供任何服务 / static
文件夹。
However after that, the application can not serve anything from the /static
folder.
有什么问题?
推荐答案
在我看来,最好使用 spring.resources.cache-period
属性来设置默认Boot <的缓存周期em>资源处理程序。因此,将以下内容添加到 application.properties
:
In my opinion, it's better to use spring.resources.cache-period
property to set the cache period of default Boot Resource Handler. So add the following to your application.properties
:
spring.resources.cache-period = 31536000
并删除 BaseMvcConfig
配置文件。
这篇关于在spring boot中为静态资源设置setCachePeriod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!