gpt4 book ai didi

java - Spring RESTful客户端: root tag exception

转载 作者:行者123 更新时间:2023-12-02 08:05:03 28 4
gpt4 key购买 nike

我正在尝试按照此示例 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类,重命名BrandListBrands (为了更接近它们引用的 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我在其中验证设置。

关于java - Spring RESTful客户端: root tag exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312573/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com