Spring RESTful客户端:root标签exception
我试图使用RestTemplate解析RESTFull调用的结果,如下示例http://thekspace.com/home/component/content/article/57-restful-clients-in-spring-3.html
XML Response就是这样的:
1111111 Test
首先,我配置了我的application-context.xml:
com.kipcast.dataModel.drugs.bean.BrandViewList
com.kipcast.dataModel.drugs.bean.BrandViewList类是一个定义了@XStreamAlias(“brand”)的bean。
我在这里如何做其余的电话:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml", WebscriptCaller.class); RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class); String url = "http://localhost:8081/alfresco/service/search/brand.xml?q={keyword}&alf_ticket={ticket}"; List results = (List) restTemplate.getForObject(url, List.class, params);
WebscriptCaller.class是我执行这些指令的类。
当我尝试执行它时,getForObject()失败并且我得到了该exception:
XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: brands
我的问题是,我该如何解决这个问题? 为什么我会遇到这种例外? 我怎么能告诉他跳过root标签?
– – – – – – – 更新 – – – – – – –
修复了一些问题,特别是:
List brandViewList = (List) restTemplate.getForObject(url, Brand.class, params);
但结果现在是:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read [class com.kipcast.dataModel.drugs.bean.Brand]; nested exception is org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.converters.ConversionException: nodeRef : nodeRef ---- Debugging information ---- message : nodeRef cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException cause-message : nodeRef class : java.util.ArrayList required-type : java.util.ArrayList converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter path : /brands/brand/nodeRef line number : 3 class[1] : com.kipcast.dataModel.drugs.bean.Brands converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter version : null -------------------------------
编辑:更新为仅包含相关信息
如果您有处理“品牌”和“品牌”标签的不同类别,那就最好了。 我会创建一个Brand
类,将BrandList
重命名为Brands
(更接近他们所指的XML部分),让Brands
持有List
。 将适当的注释放在两个类中,您应该完成,例如:
@XStreamAlias("brands") class Brands { @XStreamImplicit List brand; } @XStreamAlias("brand") class Brand { String nodeRef; String name; }
上面的代码在将对象编组到XML时非常有效,但是当您从XML解组到对象时,会失败。 为了使其正常工作,您需要告诉编组员您有哪些带注释的类:
com.kipcast.dataModel.drugs.bean.BrandViewList com.kipcast.dataModel.drugs.bean.BrandView
我创建了一个示例项目 ,我在其中validation了设置。
我使用ArrayList类型解决了这个问题。 所以不需要使用假类来处理列表。 这对我有用(没有使用任何注释):
java.util.ArrayList com.kipcast.dataModel.drugs.bean.BrandView