0

I am sending a HashMap object as response from my RestController.

@GetMapping("/balance")
    public Map<String, Object>  fetchAccountsByBalance(){
         Map<String, Object> response = new HashMap<>();
         response.put("records", totalNumberOfRecords);
         response.put("pages", totalPages);
         response.put("data", pageResponse.getContent());
}

How to control the order of JSON properties when returning a Map object?

zilcuanu
  • 3,451
  • 8
  • 52
  • 105
  • Looking at your code, I would suggest, instead of returning a map, you should return an object say Page. And in Page class, define 3 variables records,pages, and data .That is more concise and will maintain order – Ashishkumar Singh Aug 31 '21 at 05:22

1 Answers1

3

LinkedHashMap maintains insertion order:

Map<String, Object> response = new LinkedHashMap<>();
lkatiforis
  • 5,703
  • 2
  • 16
  • 35