8

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**
Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51
alvinegro
  • 81
  • 1
  • 1
  • 3
  • I did set up the property "aliases" to a map containing a set of value like "employees" and the class that is the result we want "org. ... .Employees". Hope that can help. – DomreiRoam May 16 '11 at 14:18

4 Answers4

16

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.

MaddHacker
  • 1,118
  • 10
  • 17
1

Make sure that the Marshaller and Unmarshaller that you are passing in the parameter to RestTemplate constructor has defaultImplementation set.

example:

XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().addDefaultImplementation(ArrayList.class,List.class);

XStreamMarshaller unmarshaller = new XStreamMarshaller();
unmarshaller.getXStream().addDefaultImplementation(ArrayList.class,List.class);

RestTemplate template = new RestTemplate(marshaller, unmarshaller);
sgrover
  • 41
  • 4
0

I was trying to use RestTemplate as a RestClient and following code works for fetching the list:

public void testFindAllEmployees() {
    Employee[] list = restTemplate.getForObject(REST_SERVICE_URL, Employee[].class);
    List<Employee> elist = Arrays.asList(list);
    for(Employee e : elist){
        Assert.assertNotNull(e);
    }
}

Make sure your Domain objects are properly annotated and XMLStream jar in classpath. It has to work with above condition being satisfied.

realPK
  • 2,630
  • 29
  • 22
0

I had a similar problem and solved it as in this example:

http://blog.springsource.com/2009/03/27/rest-in-spring-3-resttemplate/

Paul
  • 19,704
  • 14
  • 78
  • 96