【发布时间】:2017-03-27 11:03:56
【问题描述】:
我有一个名为 Round 的 Java 类,具有以下构造函数
public Round(){}
public Round(int id, String name, String uri) {
super();
this.id = id;
this.name = name;
this.uri = uri;
}
public Round(int id, String name, String uri, List<Stage> stages) {
this(id, name, uri);
this.stages = stages;
}
有时我需要在没有阶段的情况下获得有阶段的回合,所以我创建了一个具有两种不同 @GET 方法的 @QueryParam。
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Round> getRounds(@QueryParam("withStages") boolean withStages){
if(withStages){
return roundService.getRoundsWithStages();
}else{
return roundService.getRounds();
}
}
如果我需要有阶段的回合,我会调用:
/rounds?withStages=true
我得到这样的东西:
[
{
"id": 1,
"name": "First Round",
"stages": [
{
"id": 1,
"name": "Stage 1",
"uri": "stage1"
},
{
"id": 2,
"name": "Stage 2",
"uri": "stage2"
}
],
"uri": "firstround"
}
//and so on
如果没有阶段我调用:
/rounds?withStages=false
我得到这样的东西:
[
{
"id": 1,
"name": "First Round",
"stages": [],
"uri": "firstround"
},
{
"id": 2,
"name": "Round of 16",
"stages": [],
"uri": "roundof16"
}
//and so on
您可以看到,当我调用 ?withStages=false 时,我得到了空的阶段数组,但是,我根本不想获得它。我能做什么?
请注意,如果我在 round 类的阶段的 getter 上设置 @XmlTransient,我将无法获取第一个选项 ?withStages=true 中的阶段
我认为一个可能的解决方案是为轮次创建和扩展类,并添加阶段。但是,有没有更好的解决方案?
【问题讨论】:
-
你是在使用 Jackson 来生成 JSON 吗?
-
我正在使用 Jersey jax-rs
标签: java web-services rest jersey jax-rs