Springfox/Swagger:文档化HashMap对象

3

大家好,我正在尝试为Spring服务生成文档,其中有一个服务如下:

 @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;
  }

这里的问题是Swagger没有生成响应信息。对于POJO情况下它能正常工作,但当我返回一个哈希表时就不行了。

有人能帮忙吗?

~谢谢。

1个回答

0
当你定义了一个像这样的地图时,它的意思是在json中这是一种任意类型。具体来说,Map<String, Object>被呈现为对象。对于值的更强类型绑定,例如账户,即Map<String, Account>将产生不同的结果(与您期望的不同)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接