Spring RestTemplate和XMLStream与对象列表一起使用

alv*_*gro 8 spring xstream resttemplate

我试图用来Spring RestTemplate检索员工记录列表,例如:

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

问题是Web服务(被调用)返回以下XML格式:

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

所以当执行上面的方法时,我得到以下错误:

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**
Run Code Online (Sandbox Code Playgroud)

Mad*_*ker 16

你可能正在寻找这样的东西:

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

应该使用自动编组正确编组.


Pau*_*aul 0

我遇到了类似的问题并解决了这个问题,如下例所示:

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