简体   繁体   English

JAXB编辑列表getter?

[英]JAXB edit List getter?

I have my data model in the form of XSD files from which I then generate the corresponding Java files from xjc using command line. 我有XSD文件形式的数据模型,然后使用命令行从xjc生成相应的Java文件。

When I generate JAXB classes from an XSD, List type elements gets a getter method generated for them (with no corresponding setter method), as follows: 当我从XSD生成JAXB类时, List类型元素获取为它们生成的getter方法(没有相应的setter方法),如下所示:

public List<Type> getElement3() {
    if (element3 == null) {
        element3 = new ArrayList<Type>();
    }
    return this.element3;
}

I have lot of fields in almost every file generated from XSD of List type. 我在从List类型的XSD生成的几乎每个文件中都有很多字段。

USE case: 使用案例:

Now, I don't want the getter to be generated with the null check. 现在,我不希望使用null检查生成getter。 My application code has logic in which the getters of every fields are called often, which leads to their initialization to empty List . 我的应用程序代码具有逻辑,其中每个字段的getter经常被调用,这导致它们初始化为 List

Then while marshaling I have to stop the empty list to pass in the payload to avoid lot of empty lists being sent over the wire. 然后在编组时我必须停止空列表以传递有效负载,以避免通过线路发送大量空列表。

PS: I have a use case where Empty List is set explicitly by the user , the server has to delete certain items at the back-end. PS:我有一个用户明确设置Empty List的用例,服务器必须删除后端的某些项目。 So the differentiation whether the value is explicitly set by the user or is set just because the getter for the List was called during accessing the field . 因此,区分值是由用户显式设置还是仅因为在访问字段期间调用List的getter而设置。

So, How to make JAXB generate a getter without a null check ?? 那么,如何让JAXB生成一个没有null检查的getter?

As, editing the generated java files after compilation will be cumbersome as it's there in lot of files and we have XSD versions getting updated and would have to perform the editing every time a new version comes up. 因为在编译之后编辑生成的java文件会很麻烦,因为它存在于很多文件中,我们有XSD版本需要更新,每次新版本出现时都必须执行编辑。

At first, I would think of using a custom JAXB Binding, but I can´t think of any that would meet this requirement. 首先,我会考虑使用自定义JAXB绑定,但我不会想到任何符合此要求的内容。

In this case, perhaps you could use a wrapper class: 在这种情况下,也许您可​​以使用包装类:

import java.util.List;

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

@XmlRootElement(name = "employees")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employees
{
    @XmlElement(name = "employee")
    private List<Employee> employees = null;

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

And then define your business object: 然后定义您的业务对象:

@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee
{
    private Integer id;
    private String firstName;
    private String lastName;
    private double income;

    //Getters and Setters
}

You will have to control the initialization of the list yourself when generating the objects to be marshalled: 在生成要编组的对象时,您必须自己控制列表的初始化:

Employees employees = new Employees();
employees.setEmployees(new ArrayList<Employee>());

Source of this example: Here 这个例子的来源: 这里

I've created a small example which you could try. 我创建了一个你可以尝试的小例子。 Although it doesn't remove the null-check from the original getter, it is a jaxb-plugin which creates an additional method isXXXNull and allows you to test this case before accessing the getXXX -method. 虽然它不会从原始getter中删除null-check,但它是一个jaxb-plugin,它创建了一个额外的方法isXXXNull并允许您在访问getXXX -method之前测试这种情况。 Take a look at the example here 看看这里的例子

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

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