简体   繁体   English

使用 JAXB 为 List 生成 Setter

[英]Setter generation for List using JAXB

I use JAXB for mapping XML configuration to Java object.我使用JAXBXML配置映射到 Java 对象。 This configuration could be edited from UI , that's why in order to transfer it to UI and vice versa.这个配置可以从UI编辑,这就是为什么为了将它传输到 UI,反之亦然。 I definitely need to unmarshall json into Java object.我绝对需要将json解组为 Java 对象。 Hence it's required to have setter method for List .因此需要有List setter 方法。

How to generate setter method for List property for POJO class using JAXB?如何使用 JAXB 为 POJO 类的 List 属性生成 setter 方法?

For that you need create variable of List.The setter getter methods for this List variable are as below. 为此,您需要创建List的变量。此List变量的setter getter方法如下。

package foo.bar.me.too;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="VisitorDataList")
public class visitordatalist {


    List<visitordata> vstd;

    @XmlElement(name="VisitorData")
    public List<visitordata> getVstd() {
        return vstd;
    }

    public void setVstd(List<visitordata> vstd) {
        this.vstd = vstd;
    }   

}

For security reasons setter are not generated for List objects. 出于安全原因,不会为List对象生成setter。

/**
 * Gets the value of the dateIntervals 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 dateIntervals property.
 * 
 * <p>
 * For example, to add a new item, do as follows:
 * <pre>
 *    getDateIntervals().add(newItem);
 * </pre>
 * 
 * 
 * <p>
 * Objects of the following type(s) are allowed in the list
 * {@link DateInterval }
 * 
 * 
 */

@psyskeptic What if I don't care about security? @psyskeptic 如果我不关心安全怎么办? Is there a way to circumvent that?有没有办法绕过它?

(Posting as an answer as I'm not allowed to comment.) (作为答案发布,因为我不允许发表评论。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM