使用xstream api的例外情况

时间:2015-04-27 10:36:25

标签: java spring xstream

我正在使用提到的xml结构----

<?xml version="1.0" encoding="UTF-8"?>
<contentResponse>
    <responseStatus status="Success">
        <statusCode>0</statusCode>
    </responseStatus>
    <item type="report" contentId="1111">
        <itemsList type="extendedTOC">
            <item type="extendedTOCSectionOuter">
             <title>Executive summary</title>
             <displayOrder>1</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
             <title>Key facts</title>
             <displayOrder>2</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
             <title>Location maps</title>
             <displayOrder>3</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
             <title>Key Companies</title>
             <displayOrder>4</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Geology</title>
                <displayOrder>5</displayOrder>
                <itemsList type="">
                    <item type="extendedTOCSectionInner">
                         <title>Introduction</title>
                         <displayOrder>1</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                         <title>Structure</title>
                         <displayOrder>2</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                         <title>Reservoirs</title>
                         <displayOrder>3</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                         <title>Western Canadian Sedimentary</title>
                         <displayOrder>4</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                         <title>Athabasca Oil Sands</title>
                         <displayOrder>5</displayOrder>
                    </item>
                </itemsList>
            </item>
            <item type="Exploration">
                <title>Exploration</title>
                <displayOrder>6</displayOrder>
                <itemsList type="">
                    <item type="extendedTOCSectionInner">
                         <title>Exploration Sites</title>
                         <displayOrder>1</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                         <title>Exploration strategy</title>
                         <displayOrder>2</displayOrder>
                    </item>
                </itemsList>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Reserves and resources</title>
                <displayOrder>7</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Production</title>
                <displayOrder>8</displayOrder>
                <itemsList type="">
                    <item type="extendedTOCSectionInner">
                        <title>Oil/Liquids</title>
                        <displayOrder>1</displayOrder>
                    </item>
                    <item type="extendedTOCSectionInner">
                        <title>Production Techniques</title>
                        <displayOrder>2</displayOrder>
                    </item>
                </itemsList>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Infrastructure</title>
                <displayOrder>9</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Costs</title>
                <displayOrder>10</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Policy and Regulation</title>
                <displayOrder>11</displayOrder>
            </item>
            <item type="extendedTOCSectionOuter">
                <title>Fiscal Terms</title>
                <displayOrder>12</displayOrder>
            </item>
        </itemsList>
        <itemsList type="tables">
            <item type="tablesOuter">
                <title>Table 1</title>
                <displayOrder>1</displayOrder>
            </item>
            <item type="tablesOuter">
                <title>Table 2</title>
                <displayOrder>2</displayOrder>
            </item>
        </itemsList>
        <itemsList type="figures">
            <item type="figuresOuter">
                <title>Figure 1</title>
                <displayOrder>1</displayOrder>
            </item>
            <item type="figuresOuter">
                <title>Figure 2</title>
                <displayOrder>2</displayOrder>
            </item>
            <item type="figuresOuter">
                <title>Figure 3</title>
                <displayOrder>3</displayOrder>
            </item>
        </itemsList>
        <itemsList type="map">
            <item type="mapOuter">
                <title>dimensions</title>
                <displayOrder>1</displayOrder>
                <height>1500</height>
                <width>2000</width>
            </item>
        </itemsList>
    </item>
</contentResponse>

此外,我用来映射这些数据的xtream类定义是----

package com.woodmac.framework.adapter.cdl.model;

import java.io.Serializable;

import com.thoughtworks.xstream.annotations.XStreamAlias;


@XStreamAlias("contentResponse")
public class ContentResponse implements Serializable {

    /** The Constant serialVersionUID. */
            private static final long serialVersionUID = 4524143523466710047L;


            /** The concepts. */
    @XStreamAlias("item")
    private ContentItem contentItem;


    /** The response status. */
    @XStreamAlias("responseStatus")
    private ResponseStatus responseStatus;

    /**
     * Gets the response status.
     *
     * @return the response status
     */
    public ResponseStatus getResponseStatus() {
        return responseStatus;
    }

    /**
     * Sets the response status.
     *
     * @param responseStatus the new response status
     */
    public void setResponseStatus(ResponseStatus responseStatus) {
        this.responseStatus = responseStatus;
    }

    /**
     * Gets the content item.
     * 
     * @return the concepts
     */
    public ContentItem getContentItem() {
        return contentItem;
    }

    /**
     * Sets the content Item.
     * 
     * @param contentItem
     *            , the new concepts
     */
    public void setContentItem(ContentItem contentItem) {
        this.contentItem = contentItem;
    }
}


package com.woodmac.framework.adapter.cdl.model;

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

/**
 * The Class ContentItem.
 */
@XmlRootElement(name = "item")
public class ContentItem implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -2269696298918578400L;

    /** The concepts. */
    @XStreamAlias("itemsList")
    private List<ContentItemsList> itemsList;

    /** The analysts. */
    @XStreamAlias("analysts")
    private ContentAnalystList analysts;

    /** The related tools. */
    @XStreamAlias("relatedTools")
    private RelatedToolsList relatedTools;

    /** The related downloads. */
    @XStreamAlias("relatedDownloads")
    private RelatedDownloadsList relatedDownloads;

    /** The type. */
    @XStreamAlias("type")
    @XStreamAsAttribute
    private String type;

    @XStreamAlias("height")
    @XStreamAsAttribute
    private String height;

    @XStreamAlias("width")
    @XStreamAsAttribute
    private String width;

    /** The content id. */
    @XStreamAlias("contentId")
    @XStreamAsAttribute
    private String contentId;

    /** The content id. */
    @XStreamAlias("conceptId")
    @XStreamAsAttribute
    private String conceptId;

    /** The title. */
    @XStreamAlias("title")
    private String title;

    /** The display order. */
    @XStreamAlias("displayOrder")
    private String displayOrder;

    /** The published date. */
    @XStreamAlias("publishedDate")
    private String publishedDate;

    /** The content fragment. */
    @XStreamAlias("contentFragment")
    private String contentFragment;

    /** The preview fragment. */
    @XStreamAlias("previewFragment")
    private String previewFragment;

    /** The video url. */
    @XStreamAlias("videoUrl")
    private String videoUrl;

    /** The video description. */
    @XStreamAlias("videoDescription")
    private String videoDescription;

    /** The video published date. */
    @XStreamAlias("videoPublishedDate")
    private String videoPublishedDate;

    /** The report type. */
    @XStreamAlias("reportType")
    private String reportType;

    /**
     * Gets the related tools.
     * 
     * @return the related tools
     */
    public RelatedToolsList getRelatedTools() {
        return relatedTools;
    }

    /**
     * Sets the related tools.
     * 
     * @param relatedTools
     *            the new related tools
     */
    public void setRelatedTools(RelatedToolsList relatedTools) {
        this.relatedTools = relatedTools;
    }

    /**
     * Gets the analysts.
     * 
     * @return the analysts
     */
    public ContentAnalystList getAnalysts() {
        return analysts;
    }

    /**
     * Sets the analysts.
     * 
     * @param analysts
     *            the new analysts
     */
    public void setAnalysts(ContentAnalystList analysts) {
        this.analysts = analysts;
    }

    /**
     * Preview fragment.
     * 
     * @return the string
     */
    public String PreviewFragment() {
        return previewFragment;
    }

    /**
     * Sets the preview fragment.
     * 
     * @param previewFragment
     *            the new preview fragment
     */
    public void setPreviewFragment(String previewFragment) {
        this.previewFragment = previewFragment;
    }

    /**
     * Gets the title.
     * 
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Sets the title.
     * 
     * @param tittle
     *            the new title
     */
    public void setTitle(String tittle) {
        this.title = tittle;
    }

    /**
     * Gets the display order.
     * 
     * @return the display order
     */
    public String getDisplayOrder() {
        return displayOrder;
    }

    /**
     * Sets the display order.
     * 
     * @param displayOrder
     *            the new display order
     */
   public void setDisplayOrder(String displayOrder) {
        this.displayOrder = displayOrder;
    }

    /**
     * Gets the published date.
     * 
     * @return the published date
     */
    public String getPublishedDate() {
        return publishedDate;
    }

    /**
     * Sets the published date.
     * 
     * @param publishedDate
     *            the new published date
     */
    public void setPublishedDate(String publishedDate) {
        this.publishedDate = publishedDate;
    }

    /**
     * Gets the content fragment.
     * 
     * @return the content fragment
     */
    public String getContentFragment() {
        return contentFragment;
    }

    /**
     * Sets the content fragment.
     * 
     * @param contentFragment
     *            the new content fragment
     */
    public void setContentFragment(String contentFragment) {
        this.contentFragment = contentFragment;
    }

    /**
     * Gets the type.
     * 
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * Sets the type.
     * 
     * @param type
     *            the new type
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * Gets the content id.
     * 
     * @return the content id
     */
    public String getContentId() {
        return contentId;
    }

    /**
     * Sets the concept id.
     *
     * @param conceptId the new concept id
     */
    public void setConceptId(String conceptId) {
        this.conceptId = conceptId;
    }

    /**
     * Gets the concept id.
     * 
     * @return the concept id
     */
    public String getConceptId() {
        return conceptId;
    }

    /**
     * Sets the content id.
     * 
     * @param contentId
     *            the new content id
     */
    public void setContentId(String contentId) {
        this.contentId = contentId;
    }

    /**
     * Gets the items list.
     * 
     * @return the items list
     */
    public List<ContentItemsList> getItemsList() {
        return itemsList;
    }

    /**
     * Sets the items list.
     * 
     * @param itemsList
     *            the new items list
     */
    public void setItemsList(List<ContentItemsList> itemsList) {
        this.itemsList = itemsList;
    }

    /**
     * Gets the related downloads.
     * 
     * @return the related downloads
     */
    public RelatedDownloadsList getRelatedDownloads() {
        return relatedDownloads;
    }

    /**
     * Sets the related downloads.
     * 
     * @param relatedDownloads
     *            the new related downloads
     */
    public void setRelatedDownloads(RelatedDownloadsList relatedDownloads) {
        this.relatedDownloads = relatedDownloads;
    }

    /**
     * Gets the preview fragment.
     * @return the previewFragment
     */
    public String getPreviewFragment() {
        return previewFragment;
    }

    /**
     * Gets the video description.
     *
     * @return the video description
     */
    public String getVideoDescription() {
        return videoDescription;
    }

    /**
     * Sets the video description.
     *
     * @param videoDescription the new video description
     */
    public void setVideoDescription(String videoDescription) {
        this.videoDescription = videoDescription;
    }

    /**
     * Gets the video url.
     *
     * @return the video url
     */
    public String getVideoUrl() {
        return videoUrl;
    }

    /**
     * Sets the video url.
     *
     * @param videoUrl the new video url
     */
    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    /**
     * Gets the video published date.
     *
     * @return the video published date
     */
    public String getVideoPublishedDate() {
        return videoPublishedDate;
    }

    /**
     * Sets the video published date.
     *
     * @param videoPublishedDate the new video published date
     */
    public void setVideoPublishedDate(String videoPublishedDate) {
        this.videoPublishedDate = videoPublishedDate;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "ContentItem [itemsList=" + itemsList + ", analysts=" + analysts + ", relatedTools="
                + relatedTools + ", relatedDownloads=" + relatedDownloads + ", type=" + type
                + ", contentId=" + contentId + ", title=" + title + ", displayOrder="
                + displayOrder + ", publishedDate=" + publishedDate + ", contentFragment="
                + contentFragment + ", previewFragment=" + previewFragment + "]";
    }

            /**
             * Gets the report type.
             *
             * @return the report type
             */
            public String getReportType() {
                            return reportType;
            }

            /**
             * Sets the report type.
             *
             * @param reportType the new report type
             */
            public void setReportType(String reportType) {
                            this.reportType = reportType;
            }



}

@XmlRootElement(name = "itemsList")
public class ContentItemsList implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 6364024022629324119L;

    /** The item. */
     @XStreamImplicit
     private List<ContentItem> item;

    /** The type. */
    @XStreamAlias("type")
    @XStreamAsAttribute
    private String type;

    /**
     * get item.
     * 
     * 
     * @return item , item
     */
    public List<ContentItem> getItem() {
        return item;
    }

    /**
     * get item.
     * 
     * @param itemList
     *            , itemList
     */
    public void setItems(List<ContentItem> itemList) {
        this.item = itemList;
    }

    /**
     * get the list type.
     * @return the listType
     */
    public String getListType() {
        return type;
    }

    /**
     * set the list type.
     * @param listType the listType to set
     */
    public void setListType(String listType) {
        this.type = listType;
    }
}

但每次我尝试访问xml以将其映射到响应时我都会收到错误。

  

org.springframework.http.converter.HttpMessageNotReadableException:   无法阅读[课程   com.woodmac.framework.adapter.cdl.model.ContentResponse];嵌套   异常是org.springframework.oxm.UnmarshallingFailureException:   XStream解组异常;嵌套异常是   com.thoughtworks.xstream.converters.ConversionException:item:item

1 个答案:

答案 0 :(得分:0)

使用不正确的注释进行列表映射,应该是正确的注释 @XStreamImplicit。把它解决了这个问题