0
点赞
收藏
分享

微信扫一扫

Java:Spring Boot设置静态资源缓存方案-协商缓存

版本

<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.7.5</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>

设置示例:协商缓存

package com.example.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    // 设置静态资源映射
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 上传文件
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:public/upload/")
                .setCacheControl(CacheControl.noCache());

        // 静态资源
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl.noCache());
    }
}

参考 Spring Boot中设置静态资源不缓存

举报

相关推荐

0 条评论