如何读取resources文件夹下的一个Json文件?
How to read a Json file under the resources folder?
在我的 SpringBoot 应用程序中,我必须读取此路径 src/main/resources/mock/fileName.json 中的 Json 文件。
我是这样做的
JsonReader jsonReaderStream = new JsonReader(new InputStreamReader(Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResourceAsStream("./mock/fileName.json"))));
它在本地工作,但是当我在 Kubernetes 上部署我的 docker 文件并尝试读取我的 Json 文件时,我收到了 NullPointerException。
我使用 gradle 进行构建。我声明我是 gradle、docker 和 kubernetes 的初学者。
谁能向我解释为什么它在本地有效但在 Kubernetes 上无效?
您似乎在尝试为方法 getResourceAsStream
提供文件路径,但它并没有提供。 JavaDoc 不是很好,您必须将不同的文档拼凑在一起才能理解它,但这里有一些相关的部分:
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a '/'-separated path name that identifies the resource.
[...]
In the above, the package name for a resource is
derived from the subsequence of characters that precedes the last '/'
in the name and then replacing each '/' character in the subsequence
with '.'. A leading slash is ignored when deriving the package name.
As an example, the package name derived for a resource named
"a/b/c/foo.properties" is "a.b.c".
因此,要在 运行 系统的类路径中找到您的资源,请删除 ./
部分,使其只显示:.getResourceAsStream("mock/fileName.json")
.
这里有两种可能。
- 未打包,未找到
- 已打包,未找到
进入.jar
包,检查mock/x.json
是否存在于target\example.jar!\BOOT-INF\classes
中。
如果正在打包但未找到,请参见this answer。
new ClassPathResource("./mock/fileName.json")
然后使用 resource.getInputStream()
.
我强烈建议您将这些静态json文件放在resources/static/
下,这样您就可以直接将这些文件如resources/static/mock/a.json
作为请求/mock/a.json
在我的 SpringBoot 应用程序中,我必须读取此路径 src/main/resources/mock/fileName.json 中的 Json 文件。 我是这样做的
JsonReader jsonReaderStream = new JsonReader(new InputStreamReader(Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResourceAsStream("./mock/fileName.json"))));
它在本地工作,但是当我在 Kubernetes 上部署我的 docker 文件并尝试读取我的 Json 文件时,我收到了 NullPointerException。 我使用 gradle 进行构建。我声明我是 gradle、docker 和 kubernetes 的初学者。
谁能向我解释为什么它在本地有效但在 Kubernetes 上无效?
您似乎在尝试为方法 getResourceAsStream
提供文件路径,但它并没有提供。 JavaDoc 不是很好,您必须将不同的文档拼凑在一起才能理解它,但这里有一些相关的部分:
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a '/'-separated path name that identifies the resource.
[...]
In the above, the package name for a resource is derived from the subsequence of characters that precedes the last '/' in the name and then replacing each '/' character in the subsequence with '.'. A leading slash is ignored when deriving the package name. As an example, the package name derived for a resource named "a/b/c/foo.properties" is "a.b.c".
因此,要在 运行 系统的类路径中找到您的资源,请删除 ./
部分,使其只显示:.getResourceAsStream("mock/fileName.json")
.
这里有两种可能。
- 未打包,未找到
- 已打包,未找到
进入.jar
包,检查mock/x.json
是否存在于target\example.jar!\BOOT-INF\classes
中。
如果正在打包但未找到,请参见this answer。
new ClassPathResource("./mock/fileName.json")
然后使用 resource.getInputStream()
.
我强烈建议您将这些静态json文件放在resources/static/
下,这样您就可以直接将这些文件如resources/static/mock/a.json
作为请求/mock/a.json