0

I'm using a JSON file with thousands of rows (that serves as a "database"). It is being deserialized and then the logic of the app is based on deserialized pojos.

I'd like to enable user to switch these JSON files before running the app in the command line: (e.g.)

 java -jar target/app.jar -source=resources/data.json

The app is built with Spring Boot and I've been reading files like that:

file = ResourceUtils.getFile("/Users/App/src/main/resources/data.json");

I've found some instructions about loading properties, but what i would like to do is to load a whole file and I can't seem to find a reliable instruction.

Edit:

application.properties:

source = filepath

fragment of my DAO:

  @Repository
  public class SomethingDao {

    private ObjectMapper mapper = new ObjectMapper(); 
    private File file;

    @Value("${source}")
    private String source;

    {
        try {
            file = ResourceUtils.getFile(source);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            something = mapper.readValue(file, Something.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

commands i tried to use:

java -jar target/app.jar -source="src/main/resources/data.json"
java -jar target/app.jar -source="/data.json"
java -jar target/app.jar --source="src/main/resources/data.json"
java -jar target/app.jar --source="/data.json"
and so on...
  • Are you asking how to load a file, or are you asking how to get command-line arguments? Please do some **research**, e.g. a web search for [`spring boot command line arguments`](https://www.google.com/search?q=spring+boot+command+line+arguments) would yield some of the duplicate link above. – Andreas Apr 14 '19 at 02:05

1 Answers1

1

Achieve this by using application.properties or application.yml, specify the property in application.properties and override it through command line doc

application.properties

source = filepath

In Service or Business class

@Component
public class MyBean {

     @Value("${source}")
     private String name;

     public void someMethod() {

       file = ResourceUtils.getFile(source);
     }

  }

Command

java -jar target/app.jar -source="src/main/resources/data.json"
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Thank You for your answer. I'm getting "java.lang.IllegalArgumentException: Resource location must not be null" while trying to run the app. I've tried with -source and --source but neither seems to be working. Also my IDE tells me "Cannot resolve configuration property" pointing to the "source = filepath". Could You please help me with this? –  Apr 14 '19 at 12:22
  • Have you declared that property in application.properties @Zashiwaki – Ryuzaki L Apr 14 '19 at 12:38
  • Sorry I'm not sure I understand the whole concept of this. Am I supposed to put a specific filepath in application.properties? Like "source="src/main/resources/data.json"? Or just type the "source=filepath"? Neither works anyway. I thought I'll be able to choose the file, so the first option makes no sense. –  Apr 14 '19 at 13:02
  • Can you update the code @Zashiwaki – Ryuzaki L Apr 14 '19 at 13:39
  • Updated in edit –  Apr 14 '19 at 13:45
  • If you can, please upload the code to git and share the link, i can't overthink with this bit of code @Zashiwaki – Ryuzaki L Apr 14 '19 at 13:53
  • Thanks for trying to help me. I can't send You the code though. I've ended up doing it the 'static' way like so "ClassPathResource("data.json").getInputStream();". It can't be changed through the command line, but the data is loaded from resources folder. –  Apr 17 '19 at 13:37