java – Spring Rest Controller返回特定字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring Rest Controller返回特定字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2638字,纯文字阅读大概需要4分钟。
内容图文
我一直在思考使用Spring MVC设计JSON API的最佳方法.我们都知道IO很昂贵,因此我不想让客户端进行多次API调用以获得他们需要的东西.但与此同时,我不一定要回厨房水槽.
作为一个例子,我正在开发类似于IMDB的游戏API,而不是用于视频游戏.
如果我返回与游戏相关的所有内容,它将看起来像这样.
/ API /游戏/ 1
{
"id": 1,
"title": "Call of Duty Advanced Warfare",
"release_date": "2014-11-24",
"publishers": [
{
"id": 1,
"name": "Activision"
}
],
"developers": [
{
"id": 1,
"name": "Sledge Hammer"
}
],
"platforms": [
{
"id": 1,
"name": "Xbox One",
"manufactorer": "Microsoft",
"release_date": "2013-11-11"
},
{
"id": 2,
"name": "Playstation 4",
"manufactorer": "Sony",
"release_date": "2013-11-18"
},
{
"id": 3,
"name": "Xbox 360",
"manufactorer": "Microsoft",
"release_date": "2005-11-12"
}
],
"esrbRating": {
"id": 1,
"code": "T",
"name": "Teen",
"description": "Content is generally suitable for ages 13 and up. May contain violence, suggestive themes, crude humor, minimal blood, simulated gambling and/or infrequent use of strong language."
},
"reviews": [
{
"id": 1,
"user_id": 111,
"rating": 4.5,
"description": "This game is awesome"
}
]
}
然而,他们可能不需要所有这些信息,但他们可能会再次.从I / O和性能来看,调用所有内容似乎是一个坏主意.
我想通过在请求中指定include参数来做到这一点.
现在,例如,如果您没有指定任何包含,那么您将获得以下内容.
{
"id": 1,
"title": "Call of Duty Advanced Warfare",
"release_date": "2014-11-24"
}
但是,您希望所有信息的请求看起来像这样.
/api/game/1?include=publishers,developers,platforms,reviews,esrbRating
这样,客户端就能够指定他们想要的信息量.但是,使用Spring MVC实现这一点时,我有点不知所措.
我在想控制器看起来像这样.
public @ResponseBody Game getGame(@PathVariable("id") long id,
@RequestParam(value = "include", required = false) String include)) {
// check which include params are present
// then someone do the filtering?
}
我不确定你如何选择序列化Game对象.这甚至是可能的吗?在Spring MVC中处理此问题的最佳方法是什么?
仅供参考,我正在使用Spring Boot,其中包括Jackson进行序列化.
解决方法:
您可以将其序列化为Map< String,Object>,而不是返回Game对象,其中map键表示属性名称.因此,您可以根据include参数将值添加到地图中.
@ResponseBody
public 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"
}
]
}
内容总结
以上是互联网集市为您收集整理的java – Spring Rest Controller返回特定字段全部内容,希望文章能够帮你解决java – Spring Rest Controller返回特定字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。