简体   繁体   English

使用getter时阻止jaxb初始化列表

[英]Prevent jaxb to initialize a list when using getter

Jaxb generates this code for list type: Jaxb为列表类型生成以下代码:

public List<String> getMyList() {
    if (myList == null) {
        myList = new ArrayList<String>();
    }
    return this.myList;
}

I would like it to just return the field instead of initializing. 我希望它只返回字段而不是初始化。

How can I do it? 我该怎么做?

I'm having the same exact issue. 我有同样的问题。 I'm declaring a list in an XSD file as follows: 我在XSD文件中声明一个列表,如下所示:

<xs:element name="parameter" nillable="true" minOccurs="0" maxOccurs="999">

Resulting java class has field declared as: 产生的Java类的字段声明为:

@XmlElement(nillable = true)
protected List<ServiceType.Parameter> parameter;

But the getter: 但是吸气剂:

public List<ServiceType.Parameter> getParameter() {
    if (parameter == null) {
        parameter = new ArrayList<ServiceType.Parameter>();
    }
    return this.parameter;
}

I know that JAXB handles list as references but this clearly doesn't support XSD ability to define nillable optional list as the list will never be returned as null. 我知道JAXB将列表作为引用进行处理,但这显然不支持XSD定义可定义的可选列表的功能,因为该列表永远不会返回为null。

Do anyone know any plugin to get rid of the lazy initialization for nillable lists? 有谁知道有什么插件可以摆脱可空列表的惰性初始化?

Regards 问候

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

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