微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想通过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配置.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐