Spring RestTemplate and XMLStream use with List of Objects

alvinegro picture alvinegro · Apr 15, 2011 · Viewed 11.2k times · Source

I am trying to use Spring RestTemplate to retrieve a List of Employee records, such as:

public List<Employee> getEmployeesByFirstName(String firstName) {   
return restTemplate.getForObject(employeeServiceUrl + "/firstname/{firstName}", List.class, firstName);
}

Problem is that web services (being called), returns the following XML format:

<employees> <employee> .... </employee> <employee> .... </employee> </employees>

So when executing method above, I get following error:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read [interface java.util.List]; nested exception is org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: **employees : employees**

Answer

MaddHacker picture MaddHacker · Feb 21, 2012

You're probably looking for something like this:

public List<Employee> getEmployeeList() {
  Employee[] list = restTemplate.getForObject("<some URI>", Employee[].class);
  return Arrays.asList(list);
}

That should marshall correctly, using the auto-marshalling.