Spring RESTful客户端:root标签异常 [英] Spring RESTful client: root tag exception

查看:134
本文介绍了Spring RESTful客户端:root标签异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在此样本之后使用RestTemplate解析RESTFull调用的结果 http://thekspace.com/home/component/content/article/57-restful-clients-in-spring-3.html

I'm trying to parse the result from a RESTFull call using RestTemplate following this sample http://thekspace.com/home/component/content/article/57-restful-clients-in-spring-3.html

XML响应是这样的:

The XML Response is something like that:

<brands>
    <brand>
        <nodeRef>1111111</nodeRef>
        <name>Test</name>
    </brand>
</brands>

首先,我配置了我的application-context.xml:

For first, I configured my application-context.xml like that:

<?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的bean( 品牌)定义。

The class com.kipcast.dataModel.drugs.bean.BrandViewList is a bean with an @XStreamAlias("brand") defined.

这里我如何做剩下的电话:

Here how I do the rest call:

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是我执行这些指令的类。

WebscriptCaller.class is the class from which I execute these instructions.

当我尝试执行它时,getForObject()失败并且我得到了该异常:

When I try to execute that, the getForObject() fails and I get that exception:

XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: brands

我的问题是,我怎么能解决了吗?为什么我会遇到这种例外?我如何告诉他跳过根标签?

My question is, how can I fix that? Why I get this kind of Exception? How can I tell him to skip root tag?

--------------更新------- -------

修正了一些问题,特别是:

-------------- UPDATED --------------
fixed some issue and in particular:

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
-------------------------------


推荐答案

编辑:更新为仅包含相关信息

updated to contain only pertaining information

如果您有处理品牌和品牌标签的不同类,则最好。我会创建一个品牌类,将 BrandList 重命名为品牌 (更接近他们所指的XML部分),并让品牌持有列表< Brand> 。将适当的注释添加到两个类中,您应该完成,例如:

It's best if you have distinct classes handling "brands" and "brand" tags. I would create a Brand class, rename BrandList to Brands (to be closer to the XML parts they refer to), and let Brands hold a List<Brand>. Put the proper annotations to both classes and you should be done, e.g.:

@XStreamAlias("brands")
class Brands {
  @XStreamImplicit
  List<Brand> brand;
}

@XStreamAlias("brand")
class Brand {
  String nodeRef;
  String name;
}

上面的代码在将对象编组为XML时非常有效,但是在描述时失败了从XML解组到对象时。为了使其正常工作,您需要告诉编组人员您有哪些注释类:

The above code works perfectly when marshalling objects to XML, but fails as you describe when unmarshalling from XML to objects. For that to work fine, you need to tell the marshaller which annotated classes you have:

<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>

我创建了一个示例项目,我验证了设置。

I created a sample project where I verify the setup.

这篇关于Spring RESTful客户端:root标签异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆