如何解决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。它必须满足以上条件才能工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。