0

I am trying to upload a zipfile and load the json data from json file which is in the zipFile to respective DTO's.

upload part (following annotations are referred to spring boot annotation):

@ApiOperation(value = "Imports one or more files")
@PostMapping("/fileExchange/import")
public ResponseEntity Import(@RequestParam MultipartFile file)

Please suggest possible approaches to load json file data dynamically into respective DTO's without reading the jsonFile manually and as jsonFile length and data may vary.

  • Your question is quite broad, and feels kinda like a ["Do my work for me"](https://meta.stackoverflow.com/questions/274630/should-we-add-a-do-my-work-for-me-close-reason) question to me. Can you show us what you've tried so far? TIP: If you have no idea how to start, you can just paste the above question into ChatGPT (I got a decent answer just by using the above question, with the mention you are using Spring Boot). – Jacob van Lingen Jun 05 '23 at 09:04
  • As client confidentiality, I will not be able to share the code snipets. I tried to write the file to a /tmp/Path and read the file using fileinputSteam and converted it into String and casted it to org.json.jsonObject. It is providing me a hashmap. How can it be used to cast to DTO's . As map's cannot be casted. – hemanth reddy m Jun 06 '23 at 07:09
  • Ah owkee, so probably [Convert a Map to a POJO](https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo) is all you need. Good luck! – Jacob van Lingen Jun 07 '23 at 06:25
  • I tried this approach, it did not work. after mapping the POJO was still null, I was able to map using GSON – hemanth reddy m Jun 13 '23 at 06:50

1 Answers1

0

Initially, I tried to map the JSONObject to the POJO, using ObjectMapper's writeValue and convertValue. Though the mapping was successful in runtime without any expection, the POJO was still null.(JSON Properties were enabled to identify the JSONObjects)

I tried mapping using GSON and JSONObjects. I am able to map successfully with the below approach:

Map<String, Object> userData =
          mapper.readValue(fileData, new TypeReference<Map<String, Object>>() {});
      JSONObject json = new JSONObject((Map<String, ?>) userData.get("fundusFile"));

      FundusFile fundusFile = new Gson().fromJson(json.toString(), FundusFile.class);