0

I'm new to Spring Boot, so I'm not sure about how to store/manipulate files (use persistance within spring). Use case: Store list of films (title, director...) on a JSON file stored on API server with persistance instead of using a DB.

I have a favorites.json at src/main/resources. This file is updated when request arrives as I said. Code here: GitHub Repo

A kind person has left in the comments what is probably the problem. Changes files in classpath won't work. I still struggling how store data in JSON without a database.

Problem I'm facing:
Files are updated correctly at POST request via OutputStream, but it seems like favorites.json is treated as a static resource, so any update will be ignored until API starts again (I have tried restarting the api when the file is updated, see this but it doesn't change anything. It's still needed to stop and start manually, bash script may help, but I prefer another solution if better-possible.

Maybe I'm looking for a file-based repository, place this file in a specific project path where spring detect updates.

I think I'm skipping some important concepts of spring behaviour.

Here POST Resource

  @CrossOrigin(origins = "http://localhost:3000")
  @PostMapping(path = TaskLinks.FAVORITES, consumes = "application/json", produces = "application/json")
  @ResponseBody
  public String updateFavs(@RequestBody List<Show> newFavorites) {
    showService.updateFavorites(newFavorites);
    return "All right";
  }

Methods that modify the file:

  public boolean updateFavorites(List<Show> newFavorites) {
    if (newFavorites == null)
      return false;
    setNewFavorites(newFavorites);
    return true;
  }

  private void setNewFavorites(List<Show> newFavorites) {
    Gson gson = new Gson();
    try {
      FileWriter fileW = new FileWriter(FAVORITES_PATH);
      String strNewFavs = gson.toJson(newFavorites);
      fileW.write(strNewFavs);
      fileW.close(); // auto flush
    } catch (JsonIOException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
AdriGB
  • 35
  • 7
  • maybe this is helpful, https://stackoverflow.com/questions/43129647/intellij-idea-spring-boot-hot-reload-on-manual-save – Emerson Micu Apr 19 '21 at 09:10
  • Changes files in the classpath won't work. It might currently work but when packaged as a jar/war this will fail as you cannot change files inside the jar/war. – M. Deinum Apr 19 '21 at 09:39
  • thank you, I have edited the question, I am going to implement a database to store my json in the meantime – AdriGB Apr 19 '21 at 10:01
  • @M.Deinum I'm a spring beginner and probably mine is a dumb question, in this case it would possible put the file outside the spring boot application directory (not directly handled by the application) and everytime read and update it like suggested by the OP ? Thanks for your attention. – dariosicily Apr 19 '21 at 10:09

1 Answers1

0

If someone needs to use spring boot persistence system, I will let here what I've found.

The unique solution that I've found to use file persistance on spring-boot (API) is to hard-reload the whole API, which I think is not a clean thing. So I ended up storing the JSON file on mysql. Maybe spring have specific tools that I've omitted, but I don't have time to check right now. The closest approach I got was accessing system temporary file, which is correctly updated because it's allocated outside the application. I didn't get access to files outside the application other than temporary ones.

Now I'm working with NodeJS express and implemented a png delivery API. I don't really know how I would've done it with spring at all, but there's probably a file focused database or something that may work fine with spring. If I have to face this situation, I will upload the solution that I find most favorable. At the moment express works fine.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
AdriGB
  • 35
  • 7