Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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
Java Spring启动应用程序中的资源访问_Java_Spring_Jar - Fatal编程技术网

Java Spring启动应用程序中的资源访问

Java Spring启动应用程序中的资源访问,java,spring,jar,Java,Spring,Jar,我有一个SpringBootWeb应用程序(嵌入式Jetty服务器)。所有java代码和资源都打包在一个JAR中并部署在服务器上。但是,JAR外部有一个JSON文件,但它位于类路径中。 i、 e 我的类路径是:/usr/local/myserver/ JSON文件位于:/usr/local/myserver/hello.JSON JAR位于:/usr/local/myserver/app.JAR 当我点击URL时:它给了我“404文件未找到”。 提到的IP是所有文件和JAR都存在的服务器。我相信

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

我的类路径是:/usr/local/myserver/
JSON文件位于:/usr/local/myserver/hello.JSON
JAR位于:/usr/local/myserver/app.JAR

当我点击URL时:它给了我“404文件未找到”。 提到的IP是所有文件和JAR都存在的服务器。我相信默认的资源路径是/static/。。在罐子里。“静态”目录中的任何文件都可以访问

我已配置:

@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 
您需要使用“file://”前缀+文件系统上的绝对目标“/usr/local/myserver/”


我的类路径是:/usr/local/myserver/,我尝试过使用类路径:/,但它不起作用。同样在我上面写的代码中,我使用了Linux服务器的绝对位置,即。file://localhost/usr/local/myserver/Wow ! 我已经为此做了很多尝试;我还记得我以前也尝试过上述模式,但当时不起作用。但不知怎么的,它现在起作用了!!!谢谢
registry.addResourceHandler("/hello.json")
        .addResourceLocations("file:///usr/local/myserver/");