gpt4 book ai didi

java - Springfox/Swagger : Documenting HashMap object

转载 作者:行者123 更新时间:2023-12-03 21:22:51 24 4
gpt4 key购买 nike

friend 们,我正在尝试为 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 的情况下它工作正常,但是当我返回一个 hashmap 时它没有。

有人能帮忙吗?

~谢谢。

最佳答案

当您拥有自己定义的 map 时;这是一种表示这是 json 中的 any 类型的方式。具体Map<String, Object>被呈现为一个对象。对值的强类型绑定(bind),如 AccountMap<String, Account>会有不同的结果(就像你期待的那样)。

关于java - Springfox/Swagger : Documenting HashMap object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30885643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com