【发布时间】:2014-02-06 18:12:08
【问题描述】:
目前我正在使用 Jackson JSON 处理器从对象返回 JSON,但如果有更好的方法,我愿意提供建议。目标是定义哪些属性将包含在 JSON 响应中,具体取决于方法/url。
例子:
型号
class League {
String name;
List<Team> teams;
...
}
class Team {
String name;
String nation;
int points;
List<Player> players;
...
}
class Player {
String name;
Team team;
...
}
控制器
@Controller
public class LeagueController {
@RequestMapping(value="/league",method = RequestMethod.GET)
public @ResponseBody
League getLeague() {
//return the teams shouldn't include the player list
}
@RequestMapping(value="/team/{id}",method = RequestMethod.GET)
public @ResponseBody
Team getTeamById(Long id) {
//return team including the players but if possibe use the teamname inside the JSON
//instead of the entire back reference (which producess an infinite loop.
}
}
我查看了 Jackson 注释,但它没有让我进一步了解或提出新问题。
【问题讨论】:
-
我认为您正在寻找的是 Jackson 自定义序列化程序。
-
请检查我的答案,让我知道以寻求帮助。
标签: java json spring rest jackson