使用Spring RestTemplate

时间:2018-01-19 11:27:27

标签: java spring rest

我只想使用基于Spring的Rest Template来使用此响应。我尝试了不同的解决方案,但没有解决。

{
    "response": {
        "Status": "200",
        "data": [{
            "VehicleNo": "TS 11UB 5548",
            "lat": "17.44289",
            "lng": "78.39668",
            "timestamp": "19-01-2018 15:16:49",
            "speed": "0"
        }, {
            "VehicleNo": "TS 34T 3298",
            "lat": "17.44278",
            "lng": "78.39675",
            "timestamp": "19-01-2018 15:16:41",
            "speed": "0"
        }]
    }
}

以下是我尝试的代码

RestTemplate restTemplate = new RestTemplate();
        Response response = restTemplate.getForObject("URL:XXXXXXXX", Response.class);

响应是包含的类(“GPSEnabledVehicles” - 数据)

@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {

    private GPSEnabledVehicles response;

    public GPSEnabledVehicles getResponse() {
        return response;
    }

    public void setResponse(GPSEnabledVehicles response) {
        this.response = response;
    }
}

下面是GPSEnabledVehicles类,它具有状态和VehicleDetails列表作为属性

@JsonIgnoreProperties(ignoreUnknown = true)
public class GPSEnabledVehicles {

    private String status;
    private List<VehicleDetails> vehicleDetails;

//Setters and Getter
}

请尽快帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

public class GPSEnabledVehicles {

    private String status;
    @JsonProperty("data")   ===> this
    private List<VehicleDetails> vehicleDetails;

//Setters and Getter
}

public class GPSEnabledVehicles {

    private String status;
    private List<VehicleDetails> data;   ==> same name

//Setters and Getter
}

JsonProperty