java - 使用 docker 容器中的 java 访问类路径中的 JSON 文件时遇到 FileNotFoundException(SprintBootApplication)

标签 java json linux docker spring-boot

我在使用 docker 容器加载位于 Java jar 的类路径中的 JSON 文件时遇到 FileNotFoundException,它是一个 Spring-Boot 应用程序。此 JSON 文件在资源文件夹中可用。我能够在 ./target/classes/路径下的 docker 中看到 JSON 文件。

Resource resource = resourceLoader.getResource("classpath:folderNm/file.json");
HashMap<String, String> headerMapping = (HashMap<String, String>) parser.parse(new FileReader(resource.getFile().getAbsolutePath()));

但我得到了这个异常(exception):

java.io.FileNotFoundException: class path resource [folderNm/file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/folderNm/file.json

我试过了

-> resource.getFile().getPath(); -> resource.getFile().getCanonicalPath(); -> "./target/classes/folderName/fileName"(硬编码文件路径位置) -> "/app.jar!/folderNm/file.json"(硬编码的 FilePath 位置)

InputStream inputStream = getClass().getResourceAsStream("xyz.json");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = br.readLine()) != null)
                responseStrBuilder.append(inputStr);

上述方法均未运行。请提出解决此问题的方法。

最佳答案

假设在 maven 结构中(您使用的是 maven,对吗?)您的文件位于 src/main/resources/folderName/file.json,即您传递给 的路径getResourceAsStream 应该是 /folderName/file.json

其实一切都在javadocs :

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form: modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

基本上,如果您跳过前面的“/”,它会在您的类的包中查找 folderName。以下代码对我来说很好:

    InputStream inputStream = StackTest.class.getResourceAsStream("/folderName/file.json");
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    StringBuilder responseStrBuilder = new StringBuilder();
    String inputStr;
    while ((inputStr = br.readLine()) != null)
        responseStrBuilder.append(inputStr);
    System.out.println(responseStrBuilder.toString());

假设我的 file.json 在 src/main/resources/folderName 中。我认为这与 docker 无关。 顺便说一句,我认为您可以使用 Apache Commons IOUtils.toString这有助于将 InputStream 转换为字符串。

关于java - 使用 docker 容器中的 java 访问类路径中的 JSON 文件时遇到 FileNotFoundException(SprintBootApplication),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894788/

相关文章:

java - 将 Hibernate 3.5.x 添加到 Maven pom.xml 构建

java - 安卓:随机词生成器

javascript - 如何将带有 getter/setter 的 TypeScript 类实例转换为 JSON 以存储在 MySQL 数据库中

regex - 在 sed 中使用管道(或)

java - forName 和 Class.forName 和 isInstance 的使用

java - 如何清理谷歌应用引擎中的字符串?

javascript - JavaScript 和 Python 2.7 中 JSON 字符串中的转义反斜杠

ios - swift : How to avoid duplicate entries in CoreData for implementation with JSON array?

linux - 为什么在多个终端上同时运行同一个Python脚本时没有错误?

linux - RHEL 6 中的依赖项