gpt4 book ai didi

java - Spring Boot应用程序中的资源访问

转载 作者:行者123 更新时间:2023-12-02 16:58:12 25 4
gpt4 key购买 nike

我有一个 Spring boot Web 应用程序(嵌入式 Jetty 服务器)。所有的java代码和资源都打包在JAR中并部署在服务器上。但是,JAR 外部有一个 JSON 文件,但位于类路径中。即

my classpath is : /usr/local/myserver/
the JSON file is at : /usr/local/myserver/hello.json
the JAR is at : /usr/local/myserver/app.jar

当我点击 URL 时:https://IP:port/hello.json它给了我“404 文件未找到”。提到的 IP 是所有文件和 JAR 所在的服务器。我相信默认资源路径是/static/..,它位于 JAR 内。该“静态”目录中的任何文件都可以访问。

我已配置:

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/hello.json").addResourceLocations("file://localhost/usr/local/myserver/");
}

如何访问 JSON 文件?

最佳答案

如果“JSON”文件已在 classpath 中,那么为什么不使用 classpath: 前缀,例如

classpath:/usr/local/myserver/

如果您使用 file:/ 前缀,那么它会使用 URL 类加载 JSON 文件,所以基本上您需要提供绝对路径位置,例如

如果是windows

file:/usr/local/myserver 

关于java - Spring Boot应用程序中的资源访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com