Spring RESTful client: root tag exception

前端 未结 2 1862
花落未央
花落未央 2021-01-07 13:41

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

相关标签:
2条回答
  • 2021-01-07 14:01

    EDIT: updated to contain only pertaining information

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

    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.

    0 讨论(0)
  • 2021-01-07 14:04

    I solved this problem by using the ArrayList type. So no need to use a fake class to handle the list. It worked for me with something like this (without any annotation used) :

    <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <props>
                <prop key="brands">java.util.ArrayList</prop>
                <prop key="brand">com.kipcast.dataModel.drugs.bean.BrandView</prop>
            </props>
        </property>
    </bean>
    
    0 讨论(0)
提交回复
热议问题