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

| 我正在尝试使用ѭ0来检索Employee记录列表,例如:
public List<Employee> getEmployeesByFirstName(String firstName) {   
return restTemplate.getForObject(employeeServiceUrl + \"/firstname/{firstName}\", List.class, firstName);
}
问题是Web服务(被调用)返回以下XML格式:    ....    .... 因此,当执行上述方法时,出现以下错误:
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**
    
已邀请:
您可能正在寻找这样的东西:
public List<Employee> getEmployeeList() {
  Employee[] list = restTemplate.getForObject(\"<some URI>\", Employee[].class);
  return Arrays.asList(list);
}
应该使用自动编组正确地编组。     
确保将参数传递给RestTemplate构造函数的Marshaller和Unmarshaller设置了defaultImplementation。 例:
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);
    
我有一个类似的问题,并按以下示例所示解决了这个问题: http://blog.springsource.com/2009/03/27/rest-in-spring-3-resttemplate/     
我试图将RestTemplate用作RestClient,以下代码可用于获取列表:
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);
    }
}
确保正确注释了您的Domain对象,并在类路径中添加了XMLStream jar。它必须满足以上条件才能工作。     

要回复问题请先登录注册