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.
The RestTemplate uses the Jackson to create a Jackson java bean from the provided JSON. This bean is returned to the DataProvider. The DataProvider maps the Jackson bean to our own java bean, the data bean, and returns this to the calling application.
RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one.
So, by simply using the RestTemplateBuilder our RestTemplate will automatically use a MappingJackson2HttpMessageConverter configured with an ObjectMapper that uses the required ParameterNamesModule.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With