Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring将静态内容作为application/json提供,不管发生什么情况_Spring_Spring Boot_Caching_Static Content - Fatal编程技术网

Spring将静态内容作为application/json提供,不管发生什么情况

Spring将静态内容作为application/json提供,不管发生什么情况,spring,spring-boot,caching,static-content,Spring,Spring Boot,Caching,Static Content,我有一个简单的Spring Boot应用程序,带有静态内容。我希望浏览器缓存的不是所有类型,而是一些类型,比如css。因此,我添加了以下配置: @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/css/**").addResourceLocations("/css/") .setCac

我有一个简单的Spring Boot应用程序,带有静态内容。我希望浏览器缓存的不是所有类型,而是一些类型,比如css。因此,我添加了以下配置:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/")
                .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
}
但是现在所有的CSS文件都是服务器的内容类型:application/json。如果没有此代码,它们将用作text/css。我不知道我做错了什么。有什么我需要检查或补充的吗


提前感谢。

您必须提到文件名的扩展名,如*.css要缓存

@Configuration
public class BaseMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/css/*.css").addResourceLocations("classpath:/static/").setCachePeriod(24 * 3600 * 365); 
registry.addResourceHandler("/static/js/*.js").addResourceLocations("classpath:/static/").setCachePeriod(0); 

}
} 

我通过添加
@EnableWebMvc
注释并通过以下方式应用此配置来解决此问题:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/*.css")
            .addResourceLocations("classpath:/static/css/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/js/**")
            .addResourceLocations("classpath:/static/js/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/img/**")
            .addResourceLocations("classpath:/static/img/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/*.html")
            .addResourceLocations("classpath:/static/").setCachePeriod(0);
    registry.addResourceHandler("/*.png")
            .addResourceLocations("classpath:/static/").setCachePeriod(0);

}

@Bean
public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
}
在这种情况下,分解器很重要


我认为我在使用Spring Boot功能时犯了一些错误,而
@EnableWebMvc
帮助很大。

我说得不清楚。这些文件是用这段代码缓存的,但它们不再作为text/css,而是作为application/json。这给我带来了问题。我想你在Html中缺少type属性。你能检查一下
type=“text/css”
ex:-`试试下面的一个吗Html已经包含了:
,但我认为这不应该起作用。通过配置,
getcss/test.css
的响应头包含“contenttype:application/json”,并在一年内过期。如果删除此配置,resose头将包含“Content-type:text/css”和“Expire:0”(以及其他几个不缓存内容的头)。因此,我假设这个配置在某种程度上会弄乱mime类型,但我无法弄清楚是怎么回事。我遇到了这个确切的问题,就是当请求的文件不存在时(可能是打字错误或意外删除的文件)。当这种情况发生时,Spring会返回一个关于错误的json响应,但由于通常启用了“无嗅探”,因此在浏览器开发工具中不会看到它。