dockergradlekubernetesresourcesjsonreader

How to read a Json file under the resources folder?


In my SpringBoot application I have to read a Json file at this path src/main/resources/mock/fileName.json. I done it in this way

JsonReader jsonReaderStream = new JsonReader(new InputStreamReader(Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResourceAsStream("./mock/fileName.json"))));

It works locally but when I deploy my docker file on Kubernetes and I try to read my Json file I received a NullPointerException. I use gradle for the build. I state that I am a beginner on gradle, docker and kubernetes.

Can anyone explain to me why it works locally but not on Kubernetes ?


Solution

  • There are two possibilities here.

    1. Not Packaged, Not Found
    2. Packaged, Not Found

    Go into the .jar package, and check if your mock/x.json exists in target\example.jar!\BOOT-INF\classes.

    If it is being packaged but not found, see this answer.
    new ClassPathResource("./mock/fileName.json") and then use resource.getInputStream().


    I strongly recommend you to place these static json files under resources/static/, so you can directly have these files such as resources/static/mock/a.json, as being requested as /mock/a.json