0

我们需要在这种返回中移除基础 List 对象(equipmentMetrics):

{
"equipmentMetrics": [{
    "id": "BOSS|C5E02126",
    "reportedHours": "499.9998",
    "reportingDate": "2012-10-10"
}, {
    "id": "BOSS|C5E02126",
    "reportedHours": "499.9998",
    "reportingDate": "2012-11-10"
}]

}

我们正在使用 Jersey 和这个对象:

     @XmlRootElement(name = "equipmentMetrics")
    public class EquipmentMetricsResponse {
        private String id;
...}

而这个端点:

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("")
public List<EquipmentMetricsResponse> postV1Default(EquipmentMetricsRequest theRequest) {...}

我相信我们希望响应看起来像这样。我们只想取出:

“设备指标”:

所以我相信我们想要这个:

[{
        "id": "BOSS|C5E02126",
        "reportedHours": "499.9998",
        "reportingDate": "2012-10-10"
    }, {
        "id": "BOSS|C5E02126",
        "reportedHours": "499.9998",
        "reportingDate": "2012-11-10"
    }]

有什么帮助吗?

4

1 回答 1

1

你可以使用com.google.gson.Gson库来做到这一点。

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("")
public String postV1Default(EquipmentMetricsRequest theRequest) {
   List<EquipmentMetricsResponse> retVal ...
   ...  
   return new Gson().toJson(retVal);  
}   

返回的 JSON 是

   [{
        "id": "BOSS|C5E02126",
        "reportedHours": "499.9998",
        "reportingDate": "2012-10-10"
    }, {
        "id": "BOSS|C5E02126",
        "reportedHours": "499.9998",
        "reportingDate": "2012-11-10"
    }]
于 2012-11-15T20:34:48.473 回答