【问题标题】:Springfox/Swagger : Documenting HashMap objectSpringfox/Swagger:记录 HashMap 对象
【发布时间】:2015-06-17 08:00:33
【问题描述】:

各位,我正在尝试为 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 的情况下,它可以正常工作,但是当我返回哈希图时,它就不行了。

有人可以帮忙吗?

~谢谢。

【问题讨论】:

    标签: java documentation swagger


    【解决方案1】:

    当你有一个你定义的地图时;这是一种说法,这是 json 中的 any 类型。具体来说,Map&lt;String, Object&gt; 被渲染为一个对象。像 Account 这样的值的类型更强的类型绑定,即 Map&lt;String, Account&gt; 将产生不同的结果(如您所期望的那样)。

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 2021-01-31
      • 2019-04-14
      • 2019-04-15
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多