简体   繁体   English

Java Spring 启动 json 文件未找到异常

[英]Java Spring Boot json file not found exception

I am trying to retrieve the data from jason file into display it into console window.我正在尝试从 jason 文件中检索数据并将其显示到控制台 window 中。 The jason file is located under the folder call Test resources and with this folder I have package call sample-election-results. jason 文件位于文件夹调用测试资源下,使用此文件夹我有 package 调用示例选举结果。 But the problem is when I run the test case, it showing me following error.但问题是当我运行测试用例时,它显示了以下错误。

java.io.FileNotFoundException ""/sample-election-results/result001.json" no file found in directory. java.io.FileNotFoundException ""/sample-election-results/result001.json" 在目录中找不到文件。

Here is the screenshot of the project structure.这是项目结构的屏幕截图。

在此处输入图像描述

Here is the code to display the result into console window.这是将结果显示到控制台 window 的代码。

@Test
    public void first5Test() throws Exception {
        Scoreboard scoreboard = runTest(5);

        assertNotNull(scoreboard);
                Scoreboard sc = objectMapper.readValue(new File("/sample-election-results/result001.json"), Scoreboard.class);
                System.out.println(sc);

        
            
    }

Here is the screenshot of the error message.这是错误消息的屏幕截图。

错误信息

you can use ClassLoader.getSystemResourceAsStream("you file path") load a file.您可以使用ClassLoader.getSystemResourceAsStream("you file path")加载文件。

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

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