Springfox/Swagger : Documenting HashMap object

1.4k views Asked by At

People, I am trying to generate documentation for the spring services and I have one service which is like

 @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

  public ResponseEntity<Map<String, Object>> getAccountList(
    HttpServletRequest request, HttpServletResponse response,
      ) {

    // Get account associate with user.
    List<Account> accounts =
        accountDB.findAccountForUser(user.getId());

    // Create response object.
    Map<String, Object> responseObject = new LinkedHashMap<String, Object>();
    responseObject.put(StringConstants.RESPONSE_TYPE, StringConstants.SUCCESS);
    responseObject.put(StringConstants.STATUS, HttpStatus.OK.value());
    responseObject.put(StringConstants.ITEMS, mapAccountList(accounts));

    return new ResponseEntity<Map<String, Object>>(responseObject, HttpStatus.OK);
  }

  private List<Map<String, String>> mapAccountList(List<Account> accountList) {
    List<Map<String, String>> accountMapList = new ArrayList<Map<String, String>>();
    // Iterate over accountList and create map.
    for (Account account : accountList) {
      Map<String, String> accountMap = mapAccount(account);
      accountMapList.add(accountMap);
    }
    return accountMapList;
  }

Here, the problem is swagger is not generating a response information. In a case of POJO it works fine, but when I return a hashmap it does not.

Can any one help?

~Thanks.

1

There are 1 answers

0
Dilip Krishnan On

When you have a map like you've defined it; it is a way of saying this is an any type in json. Specifically Map<String, Object> is rendered as an object. A more strongly typed type binding for the value like Account i.e Map<String, Account> will have different results (like what you're expecting).