2

I'm trying to build a Springboot app that allows to insert Json object from Postman and save it to existing json file that has other data. I'm new to Jackson so perhaps I missed something?

this is how my json file looks:

[
   {
      "Name":"After Dark",
      "Author":"Haruki Murakami"
   },
   {
      "Name":"It",
      "Author":"Stephen King"
   }
]

This is what I have tried:

@PostMapping("/insertBook")
public void insertBook(@RequestBody Book book)  {
    File booksJsonFile = Paths.get(this.getClass().getResource("/books.json").toURI()).toFile();
    objectMapper.writeValue(booksJsonFile, book);
}

It's inserts to an empty file but it's doesn't append to existing json file.

I also have tried this:

@PostMapping("/insertBook")
public void insertBook(@RequestBody Book book) throws URISyntaxException {

    try {
        File file = Paths.get(this.getClass().getResource("/books.json").toURI()).toFile();
        FileWriter fileWriter = new FileWriter(file, true);
        SequenceWriter seqWriter = objectMapper.writer().writeValuesAsArray(fileWriter);
        seqWriter.write(book);

        seqWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is what I'm sending from Postman: enter image description here

Do I need to use something else to achieve the result that I want? I will be thankfull for your help.

Lukasz Blasiak
  • 642
  • 2
  • 10
  • 16
MayWheather
  • 476
  • 5
  • 18

1 Answers1

5

I have tried to reproduce your problem according to your code and I come to following conclusions:

  1. You can not modify file under resources directly. Here is explanation why.

  2. I managed to append new JSON to the file (using your approach but saving file locally) but it's probably not what you expect (json structure is corrupted):

[
   {
      "Name":"After Dark",
      "Author":"Haruki Murakami"
   },
   {
      "Name":"It",
      "Author":"Stephen King"
   }
][{"Name":"new name","Author":"new author"}]

I am afraid that it is not possible to update current JSON structure directly in the file.

  1. I managed to solve your problem using org.json library. However, the disadvantage of my solution is necessity of rewriting entire file each time. In addition I used synchronized keyword in order to avoid simultaneous file modification.
public synchronized void updateJsonFile(Book book) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    Path path = Paths.get("./books.json");
    final String currentJsonArrayAsString = Files.readString(path);

    try (FileWriter fileWriter = new FileWriter(path.toFile(), false)) {

        JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(book));
        JSONArray jsonArray = new JSONArray(currentJsonArrayAsString);
        jsonArray.put(jsonObject);

        fileWriter.write(jsonArray.toString());
    }
}

And now the books.json has following content:

[
   {
      "Author":"Haruki Murakami",
      "Name":"After Dark"
   },
   {
      "Author":"Stephen King",
      "Name":"It"
   },
   {
      "Author":"new author",
      "Name":"new name"
   }
]
Lukasz Blasiak
  • 642
  • 2
  • 10
  • 16