除了返回
Game对象外,您还可以将其序列化为
Map<String,Object>,其中映射键代表属性名称。因此,您可以根据
include参数将值添加到地图中。
@ResponseBodypublic Map<String, Object> getGame(@PathVariable("id") long id, String include) { Game game = service.loadGame(id); // check the `include` parameter and create a map containing only the required attributes Map<String, Object> gameMap = service.convertGameToMap(game, include); return gameMap;}例如,如果您有一个
Map<String, Object>类似这样的人:
gameMap.put("id", game.getId());gameMap.put("title", game.getTitle());gameMap.put("publishers", game.getPublishers());它将像这样被序列化:
{ "id": 1, "title": "Call of Duty Advanced Warfare", "publishers": [ { "id": 1, "name": "Activision" } ]}


