java-Spring RESTful客户端:根标签异常
作者:互联网
我正在尝试在此示例http://thekspace.com/home/component/content/article/57-restful-clients-in-spring-3.html之后使用RestTemplate解析RESTFull调用的结果
XML响应是这样的:
<brands>
<brand>
<nodeRef>1111111</nodeRef>
<name>Test</name>
</brand>
</brands>
首先,我像这样配置了application-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xstreamMarshaller" />
<property name="unmarshaller" ref="xstreamMarshaller" />
</bean>
</list>
</property>
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="brand">com.kipcast.dataModel.drugs.bean.BrandViewList</prop>
</props>
</property>
</bean>
</beans>
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<BrandViewList> results = (List<BrandViewList>) restTemplate.getForObject(url, List.class, params);
WebscriptCaller.class是我从中执行这些指令的类.
当我尝试执行该操作时,getForObject()失败,并得到该异常:
XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: brands
我的问题是,我该如何解决?为什么会出现这种异常?我如何告诉他跳过根标记?
– – – – – – – 更新 – – – – – – –
解决了一些问题,尤其是:
List<Brand> brandViewList = (List<Brand>) 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< Brand>.在两个类上都添加正确的注释,然后应该完成操作,例如:
@XStreamAlias("brands")
class Brands {
@XStreamImplicit
List<Brand> brand;
}
@XStreamAlias("brand")
class Brand {
String nodeRef;
String name;
}
上面的代码在将对象编组为XML时可以完美地工作,但是当您描述从XML到对象进行编组时,上述代码会失败.为了使它正常工作,您需要告诉编组器您拥有哪些带注释的类:
<bean name="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="annotatedClasses">
<array>
<value>com.kipcast.dataModel.drugs.bean.BrandViewList</value>
<value>com.kipcast.dataModel.drugs.bean.BrandView</value>
</array>
</property>
</bean>
我创建了一个sample project,用于验证设置.
标签:alfresco,java,spring,rest,xml 来源: https://codeday.me/bug/20191009/1882926.html