加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring在文件名中使用dot(s)提供静态内容

发布时间:2020-12-15 01:30:13 所属栏目:大数据 来源:网络整理
导读:我想通过Spring提供npm中构建的网页,一切都运行正常,但无论真正的后缀是什么(css,js或html),我都无法提供像main.xxxx.yyy这样的资源. 目录树是这样的: src/main/resource/resource index.html asset-manifest.json favicon.ico manifest.json service-worke

我想通过Spring提供npm中构建的网页,一切都运行正常,但无论真正的后缀是什么(css,js或html),我都无法提供像main.xxxx.yyy这样的资源.

目录树是这样的:

src/main/resource/resource
                  index.html
                  asset-manifest.json
                  favicon.ico
                  manifest.json
                  service-worker.js
                  static
                     css
                         main.fc656101.css
                         main.fc656101.css.map
                     js
                         main.91794276.js
                         main.91794276.js.map
                     media
                         banner.bdcf92f4.jpg
                         fontawesome-webfont.912ec66d.svg
                         ...

这是应用程序类:

@SpringBootApplication
public class Application {
   private static Logger log=Logger.getLogger(Application.class.getName());

@Bean
WebMvcConfigurer configurer () {
    return new WebMvcConfigurerAdapter() {

        @Override
        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/static/*").
                      addResourceLocations("classpath:/static/");
        }
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            super.configurePathMatch(configurer);

            configurer.setUseSuffixPatternMatch(false);
        }
    };
}

public static void main(String[] args) {

    SpringApplication.run(Application.class,args);
}

为了调试这个问题,我手动重命名了一些文件并且它可以工作,因此我将问题限制在包含点的文件名中.

我看到有人解决了添加{variable:.在RestControllers中的请求映射,但我没有控制器,所以我无法弄清楚如何做到这一点.

编辑:

我发现这个配置:

@Configuration
class ServletConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(final PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
        configurer.setUseTrailingSlashMatch(false);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

}

现在它提供所有* .html,包括page.01.html,但仍然不是style.01.css或script.01.js.我假设是一个不同的问题,原始的问题由ContentNegotiationConfigurer解决.

最佳答案
我写的这应该是一个非常愚蠢的问题……

问题是浏览器缓存和项目清理.确保始终清除缓存(这是非常明显的),但也要在更改配置后清除项目中提供静态内容的位置.停止并重新启动JAVA是不应该的.

这花费了我三天,但现在正在工作,正确的配置是我发布的第一个,不需要contentNegotiation配置.

希望这可以为其他人节省一天!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读