-1

I have 3 json files with combined size of 150mb and it has some data. In local , i stored it in resources folder and reading after application start up and writing to mongodb if the collection not exists.

When I deploy this to cloud, I am seeing null because files moved to BOOT-INF/classes inside a jar. I need suggestions on below queries.

  1. How to write a common method which supports the file read local run vs cloud run ? As I am facing issues while running in cloud ? I tried as below

    @Bean
        @Profile("cloud")
        public Resource[] getCloudResources() throws IOException {
            Resource[] resources = applicationContext.getResources("classpath*:*.json");
            return resources;
        }
    
        @Bean
        @Profile("!cloud")
        public Resource[] getNonCloudResources() throws IOException {
            Resource[] resources = applicationContext.getResources("*.json");
            return resources;
        }
    
  2. It seems to be not the best practice to store these files in resources folder. What's the alternative approach according to 12 factors standard ? I am deploying in PCF

  3. Please suggest any other approach if you came across the similar use case.
James Z
  • 12,209
  • 10
  • 24
  • 44
J.K
  • 11
  • 3
  • Jackson is the default JSON library used by Spring Boot. Under the hood, Spring Boot is using Jackson lib to/from convert JSON<->POJOs. Check out this great post on similar topic: https://springframework.guru/jackson-annotations-json/ – nabster Mar 06 '20 at 19:24

1 Answers1

0
  1. A common method would be the one that accepts an InputStream parameter and reads the file from it. If the file is in classpath then you can use getResourceAsStream (here) to get the InputStream. If not, you can use respective cloud API's method(s) that return InputStream to a resource stored in cloud.
  2. Yes, that's correct. Files should not be the part of the application artefact. App should accept the file path as a configurable property (in application.properties for spring boot) and read the file from it.
  3. Having an app reading a file on startup is a good idea. Although it adds a bit of latency to the start up, it should be fine as you would not want the app to serve the requests unless it has loaded the data.
  4. As far as reading json objects from file is concerned, you can have a look at Jackson library. Here is the documentation and here is an example.
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Hi Darshan, Thanks for your inputs. I resolved the local vs cloud file issue now using getResourceAsStream. Coming to #2, should I upload the .json files to my git repo and refer the url in application properties and write a method to download the file and process it ? – J.K Mar 06 '20 at 18:45
  • The file should not be present in the git repo as it's a config file and might contain sensitive information. You can deploy it wherever the app is deployed so that app can access it. – Darshan Mehta Mar 13 '20 at 13:08
  • Got it. Thanks Darshan – J.K Apr 08 '20 at 18:31