简体   繁体   English

我正在尝试读取一个简单的 Json 文件,但我保存了一个 null。 我究竟做错了什么? 我的 json 仅包含 4 个字段

[英]I'm trying to read a simple Json file but im getting saved a null. What am i doing wrong? My json contains only 4 fields

Im trying to write a micrservice to read json using jackson mapper by using below code我试图通过使用下面的代码使用 jackson 映射器编写一个 micrservice 来读取 json

      @Bean
    CommandLineRunner runner(ResponseService responseService) {
        return args -> {
            // read JSON and load json
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<Response>> typeReference1 = new TypeReference<List<Response>>() {};
            InputStream inputStream1 = TypeReference.class.getResourceAsStream("/json/response.json");
            try {
                List<Response> responses = mapper.readValue(inputStream1, typeReference1);
                responseService.save(responses);
                System.out.println(responses);
                System.out.println("response saved");
            } catch (IOException e) {
                System.out.println("not saved" + e.getMessage());
            }

        };
    }

    @Service
public class ResponseService {
    private ResponseRepository responseRepository;

    public ResponseService(ResponseRepository responseRepository) {
        this.responseRepository = responseRepository;
    }

    public Iterable<Response> list() { return responseRepository.findAll();
    }

    public void save(List<Response> users) {
        responseRepository.saveAll(users);
    }
}

As you stated in comment your path is:正如您在评论中所说,您的路径是:

C:\Users\Desktop\spring-boot-jsontodb-master\src\main\resources\json\response.json

So instead of "/json/response.json" you should use: json/response.json因此,您应该使用: json/response.json而不是"/json/response.json"

InputStream inputStream1 = TypeReference.class.getResourceAsStream("json/response.json");

just delete the first slash.只需删除第一个斜杠。

暂无
暂无

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

相关问题 我的名单总是 Null。我做错了什么? - My list always Null. What am I doing wrong? 我收到看似简单的 Java 代码的奇怪错误。 我在这里做错了什么? - I'm getting weird errors for seemingly simple Java code. What am I doing wrong here? 我在加粗的代码行中收到 NULL 指针异常? 我究竟做错了什么? - I'm getting a NULL POinter Exception at the boldened lines of code! What am I doing wrong? 我的arraylist出现IndexOutOfBoundsException,我在做什么错? - I'm getting an IndexOutOfBoundsException with my arraylist, what am I doing wrong? 尝试从文件中读取数据后创建HashMap。 获取空值。 我究竟做错了什么? - Trying to create a HashMap after reading data from file. Getting null values. What am I doing wrong? 我正在尝试将一个值从 servlet 文件传递​​到 javascript 文件,但我一直为 null 我做错了什么? - I am trying to pass a value from servlet file to javascript file and i am keep getting null where am i doing wrong? 我在简单的 android 应用程序中做错了什么? - What am i doing wrong at my simple android app? 我简单的Spring Security应用程序在做什么错 - What am I doing wrong in my simple spring security app 异步json解析-我在做什么错? - Async json parsing- What am i doing wrong? 我正在尝试制作一个程序,将 object 保存到文件中,然后从文件中读取 object。 我究竟做错了什么? - I'm trying to make a progrm that saves an object to a file and then reads the object from the file. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM