java Spring Rest Controller 返回特定字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30558784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Rest Controller Return Specific Fields
提问by greyfox
I've been going through my head the best way to design a JSON API using Spring MVC. As we all know IO is expensive, and thus I don't want to make the client make several API calls to get what they need. However at the same time I don't necessarily want to return the kitchen sink.
我一直在思考使用 Spring MVC 设计 JSON API 的最佳方法。众所周知,IO 是昂贵的,因此我不想让客户端进行多次 API 调用来获得他们需要的东西。然而,与此同时,我不一定要归还厨房水槽。
As an example I was working on a game API similar to IMDB but for video games instead.
例如,我正在开发类似于 IMDB 的游戏 API,但用于视频游戏。
If I returned everything connected to Game it would look something like this.
如果我返回所有与 Game 相关的东西,它看起来会像这样。
/api/game/1
/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"
}
]
}
However they may not need all this information, but then again they might. Making calls for everything seems like a bad idea from I/O and performance.
然而,他们可能不需要所有这些信息,但他们可能需要。从 I/O 和性能来看,对所有内容进行调用似乎是一个坏主意。
I thought about doing it by specifying include parameter in the requests.
我想通过在请求中指定包含参数来实现。
Now for example if you did not specify any includes all you would get back is the following.
现在,例如,如果您没有指定任何包含,您将返回以下内容。
{
"id": 1,
"title": "Call of Duty Advanced Warfare",
"release_date": "2014-11-24"
}
However it you want all the information your requests would look something like this.
但是,如果您希望请求的所有信息看起来像这样。
/api/game/1?include=publishers,developers,platforms,reviews,esrbRating
This way the client has the ability to specify how much information they want. However I'm kind of at a loss the best way to implement this using Spring MVC.
通过这种方式,客户可以指定他们想要多少信息。但是,我有点不知所措,这是使用 Spring MVC 实现这一点的最佳方式。
I'm thinking the controller would look something like this.
我认为控制器看起来像这样。
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?
}
I'm not sure how you would optionally serialize the Game object. Is this even possible. What is the best way to approach this in Spring MVC?
我不确定您将如何选择性地序列化 Game 对象。这甚至可能吗。在 Spring MVC 中解决这个问题的最佳方法是什么?
FYI, I am using Spring Boot which includes Hymanson for serialization.
仅供参考,我正在使用包含 Hymanson 的 Spring Boot 进行序列化。
采纳答案by Marlon Bernardes
Instead of returning a Game
object, you could serialize it as as a Map<String, Object>
, where the map keys represent the attribute names. So you can add the values to your map based on the include
parameter.
Game
您可以将其序列化为 a Map<String, Object>
,而不是返回一个对象,其中映射键代表属性名称。因此,您可以根据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;
}
As an example, if you have a Map<String, Object>
like this:
举个例子,如果你有Map<String, Object>
这样的:
gameMap.put("id", game.getId());
gameMap.put("title", game.getTitle());
gameMap.put("publishers", game.getPublishers());
It would be serialized like this:
它会像这样序列化:
{
"id": 1,
"title": "Call of Duty Advanced Warfare",
"publishers": [
{
"id": 1,
"name": "Activision"
}
]
}
回答by yaccob
Being aware that my answer comes quite late: I'd recommend to look at Projections
.
意识到我的答案来得很晚:我建议查看Projections
.
What you're asking for is what projections are about.
您要求的是预测的内容。
Since you're asking about Spring I'd give this one a try: https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
既然你问的是 Spring,我想试试这个:https: //docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts