Reputation: 5770
I want to use Spring's RestTemplate plus Jackson to consume a WebService. I have followed several tutorials and have come to the point of creating the DAOs. This is the method where I get all of my domain objects:
// Create a Rest template
RestTemplate restTemplate = new RestTemplate();
// Create a list for the message converters
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
// Add the Jackson Message converter
messageConverters.add(new MappingJacksonHttpMessageConverter());
// Add the message converters to the restTemplate
restTemplate.setMessageConverters(messageConverters);
List<Station> resultList = Arrays.asList(restTemplate.getForObject(BASE_URL, Station[].class));
return resultList;
But my Web Service does not return an array of Station objects right away, but rather a more semantic expression in this way:
{"success":true,"message":"Records Retrieved Successfully","data":{"totalCount":"14","stations":[{"id":"1264","station":"Station 1","idJefatura":"1","syncDate":"2013-01-24 13:20:43"}, ...] }}
So my problem is, I'm not sure how to "tell" RestTemplate to parse the object list right after the "stations" indicator, without creating an ad hoc object, which does not seem like the proper solution.
Is there any way to specify the right syntax for RestTemplate?
EDIT: I created a wrapper object like this:
public class RestResponseObject {
private boolean success;
private String message;
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public class Data {
private int totalCount;
private List<Station> stations;
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<Station> getStations() {
return stations;
}
public void setStations(List<Station> estaciones) {
this.stations= estaciones;
}
}
}
But I am struggling as to how to make this object generic, since the key name of my object list in the JSON response is dependant of that domain object's class.
Upvotes: 10
Views: 33434
Reputation: 5749
There are two solutions here:
Here is an example.
The response class
public class MyResponseClass {
// other variables
private List<Station> stations; //it getters and setters
}
In the Rest Client
MyResponseClass response = restTemplate.getForObject(BASE_URL, MyResponseClass.class)
List<Station> resultList = response.getStations()
Upvotes: 4