java mvc返回字段_java – Spring Rest Controller返回特定字段

我一直在思考使用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进行序列化.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值