繁体   English   中英

Spring Boot服务静态资源

[英]Spring boot serving static resource

我致力于Spring Boot应用程序。 我正在尝试使用Spring提供静态内容。 希望在每次请求匹配以下格式的URL时提供服务在/ c:/ frontend / files /目录中存储的资源:/ file / **:

 @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry
            .addResourceHandler("/file/**")
            .addResourceLocations("file:///C:/frontend/files/" );

}

但是当我尝试使用以下URL访问此资源时: http:// localhost:9999 / file / app.min.js

我有这个问题

There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

我解决了这个问题。 它与“ spring-cloud-config-server”有关。 我只是删除此配置:org.springframework.cloud spring-cloud-config-server

听起来您的项目的文件夹结构错误。

代码应位于src/main/java ,资源(如您的JavaScript)应位于src/main/resources 您有一些实际可以从中提供文件的选项。 spring.io博客上的这篇文章如下:

Spring Boot将自动添加位于以下任何目录中的静态Web资源:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

您还可以选择使用webjars

就个人而言,我发现将这类文件放在src/main/resources/public下是最容易的。 对我来说,它始终没有任何问题。 有趣的是,您可以在项目中的任何位置放置一个名为/public的文件夹,而spring-boot将从中提供文件。 您必须非常小心,它位于src/main/resources/public但是如果使用的是诸如maven之类的构建工具,则当您构建.jar时,文件将不在正确的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM