JAXB编辑列表getter? [英] JAXB edit List getter?

查看:153
本文介绍了JAXB编辑列表getter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有XSD文件形式的数据模型,然后使用命令行从 xjc 生成相应的Java文件。

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生成JAXB类时, List 类型元素获取为它们生成的getter方法(没有相应的setter方法),如下所示:

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:

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

我在XSD的每个文件中都有很多字段列表类型。

I have lot of fields in almost every file generated from XSD of List type.

USE案例:

现在,我不希望使用 null 检查生成getter。我的应用程序代码具有逻辑,其中每个字段的getter经常被调用,这导致它们初始化为 empty List

Now, I don't want the getter to be generated with the null check. My application code has logic in which the getters of every fields are called often, which leads to their initialization to empty 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:我有一个用例空列表由用户明确设置,服务器必须删除某些后端的物品。因此,区分值是由用户显式设置还是仅因为访问字段期间调用列表的getter而设置。

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. 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.

那么,如何让JAXB生成一个没有 null的getter check ??

So, How to make JAXB generate a getter without a null check ??

因为在编译之后编辑生成的java文件会很麻烦,因为它存在于很多文件中,我们有XSD版本需要更新,并且每次都需要执行编辑新版本出现了。

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.

推荐答案

首先,我会考虑使用自定义的JAXB Binding,但我想不出任何可以满足这个要求的要求。

At first, I would think of using a custom JAXB Binding, but I can´t think of any that would meet this requirement.

在这种情况下,也许你可以使用包装类:

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>());

此示例的来源:这里

这篇关于JAXB编辑列表getter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆