json list在春天没有被分配密钥

时间:2013-08-05 05:47:15

标签: json spring rest

我正在向REST服务发布JSON请求。 service生成一个json列表(将bean列表对象转换为JSON)。但它不包含在客户端解析此列表的密钥。 我可以在客户端没有密钥解析json列表,或者如何为此列表分配密钥。

@RequestMapping(value="/getScheduleTrip", method=RequestMethod.POST, consumes="application/json" )  
public @ResponseBody List<ResponseParametersOfRest> getScheduleTrip(@org.springframework.web.bind.annotation.RequestBody RequestParametersOfRest requestRest) {  
    List<ResponseParametersOfRest> responseRest = null;
    try{
        System.out.println("hello");
        responseRest = serviceScheduledTripJDBCTemplate.getTripDetails(requestRest);
    }catch(Exception ee){
        ee.printStackTrace();
    }
    return responseRest;  


}

,输出

[{"companyID":8,"deviceID":18,"vehicleID":18,"personnelID":172,"startCity":"Jaipur","startState":"ND","startDate":"2013-08-14","startTime":"10:55:00","endCity":"ajmer","endState":"CM","endDate":"2013-08-05","endTime":"10:55:00","manifestNo":"7568956","initialCargoWeight":"0","yardLocation":"","loadStatus":false,"scheduledTripUserID":42,"instructions":null,"scheduledTripStatus":false,"receivedDateTime":"2013-08-05 10:58:33.0","serverScheduledTripID":42,"deleteStatus":false,"errorCode":0}]

1 个答案:

答案 0 :(得分:1)

可以在不将数组分配给属性的情况下解析数组,但不建议这样做。

http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx

最好将它分配给一个属性。为此,您可以创建一个新类,例如

class ListJsonResource {
    private final List<Object> list;

    public ListJsonResource(List<Object> list) { ... }
    public List<Object> getList() { ... }
}

这将包装你的列表并改变输出。

public @ResponseBody ListJsonResource getScheduleTrip(@RequestBody RequestParametersOfRest requestRest) {  
    List<ResponseParametersOfRest> responseRest = null;
    try{
        responseRest = serviceScheduledTripJDBCTemplate.getTripDetails(requestRest);
    }catch(Exception ee){
        ee.printStackTrace();
    }

    return new ListJsonResource(responseRest);  
}