在spring boot中为静态资源设置setCachePeriod [英] Set setCachePeriod for static resources in spring boot

查看:1219
本文介绍了在spring boot中为静态资源设置setCachePeriod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆