【问题标题】:Mapping an ArrayList of Object with XSD file使用 XSD 文件映射对象的 ArrayList
【发布时间】:2012-05-04 15:49:35
【问题描述】:

我正在使用 Java/Eclipse/Spring 创建 Web 服务。我正在编写一个 XSD 文件,其中包含用于 Web 服务(请求、响应等)的 Java 类的结构。 我在 XSD 文件上使用“生成 Jaxb 类”选项来创建 Java 类。 它适用于 int、string、long 等数据类型,但是,当我的属性是对象的 ArrayList 时,生成的 Java 类没有该属性的“SET”方法。它只有“GET”方法。

这是一个例子:

XSD 文件:

<element name="GetPrizesAndCatalogsResponse">
    <complexType>
        <sequence>
            <element name="answerCode" type="int" />
            <element name="prizes" type="tns:SW_Prize" maxOccurs="unbounded"></element>
            <element name="prizesCatalog" type="tns:SW_Catalog" maxOccurs="unbounded"></element>            
            <element name="pagination" type="tns:SW_Pagination"></element>          
        </sequence>
    </complexType>
</element>

Java 类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "answerCode",
    "prizes",
    "prizesCatalog",
    "pagination"
})
@XmlRootElement(name = "GetPrizesAndCatalogsResponse")
public class GetPrizesAndCatalogsResponse {

    protected int answerCode;
    @XmlElement(required = true)
    protected List<SWPrize> prizes;
    @XmlElement(required = true)
    protected List<SWCatalog> prizesCatalog;
    @XmlElement(required = true)
    protected SWPagination pagination;

    /**
     * Obtiene el valor de la propiedad answerCode.
     * 
     */
    public int getAnswerCode() {
        return answerCode;
    }

    /**
     * Define el valor de la propiedad answerCode.
     * 
     */
    public void setAnswerCode(int value) {
        this.answerCode = value;
    }

    /**
     * Gets the value of the prizes property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the prizes property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getPrizes().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link SWPrize }
     * 
     * 
     */
    public List<SWPrize> getPrizes() {
        if (prizes == null) {
            prizes = new ArrayList<SWPrize>();
        }
        return this.prizes;
    }

    /**
     * Gets the value of the prizesCatalog property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the prizesCatalog property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getPrizesCatalog().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link SWCatalog }
     * 
     * 
     */
    public List<SWCatalog> getPrizesCatalog() {
        if (prizesCatalog == null) {
            prizesCatalog = new ArrayList<SWCatalog>();
        }
        return this.prizesCatalog;
    }

    /**
     * Obtiene el valor de la propiedad pagination.
     * 
     * @return
     *     possible object is
     *     {@link SWPagination }
     *     
     */
    public SWPagination getPagination() {
        return pagination;
    }

    /**
     * Define el valor de la propiedad pagination.
     * 
     * @param value
     *     allowed object is
     *     {@link SWPagination }
     *     
     */
    public void setPagination(SWPagination value) {
        this.pagination = value;
    }

}

我对 XSD 文件的定义是否正确?是否需要在 XML 上添加另一个标签?

【问题讨论】:

    标签: java web-services jaxb


    【解决方案1】:

    已解决:

    有一种新方法(我不知道是不是新方法)叫做“addAll”。 您可以获取您的收藏,然后使用 addAll 添加另一个收藏。

    代码:

    getPrizesAndCatalogsResponse.getPrizes().addAll(newPrizes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2015-05-22
      • 2019-09-19
      • 2019-09-24
      • 2016-04-01
      • 2018-01-18
      相关资源
      最近更新 更多