首页 > 解决方案 > JAXB (MOXy) - 解组空值

问题描述

我在使用 JAXB MOXy 解组 JSON 时遇到问题。

下面是我要解析的 JSON:

{
    "accounts": [{
        "description": "A",
        "balance": 1000,
        "balanceAvailable": 1000
    },
    {
        "description": "B",
        "balance": 1001,
        "balanceAvailable": 1001
    },
    {
        "description": "C",
        "balance": 1002,
        "balanceAvailable": 1002
    }],
    "cardPermissions": null,
    "totalBalanceAvailable": 1046.19
}

这里的问题在于字段“ cardPermissions”。

我正在尝试解组一个没有“ cardPermissions”字段(没有 @XmlElement 注释和类属性)的对象。

JAXB 在解组此字符串时抛出 NullPointerException,因为它是:

"cardPermissions": null

并不是:

"cardPermissions": "null"

要接受空值,我必须在我的 POJO 中添加字段 cardPermissions(带有 @XmlElement 注释)。

这样做,同样没有 getter 和 setter,JAXB 可以正确地解组提供的 JSON。

null 和“null”是完全不同的东西,但我不想在我的 POJO 中包含这个字段,我必须忽略这些 null 值。

编辑

如果我像这样包含根元素(“ customerInfo”):

{
    "customerInfo": {
        "accounts": [{
            "description": "B",
            "balance": 1000,
            "balanceAvailable": 1000
        },
        {
            "description": "C",
            "balance": 1001,
            "balanceAvailable": 1001
        },
        {
            "description": "D",
            "balance": 1002,
            "balanceAvailable": 1002
        }],
        "cardPermissions": null,
        "totalBalanceAvailable": 1046.19
    }
}

JSON被正确解析,但我不想在解组json时包含root。

这是我的 POJO:

@XmlRootElement(name = "customerInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerInfo {

    @XmlElement(name = "name")
    private String firstName;

    @XmlElement(name = "surname")
    private String lastName;

    @XmlElement
    private String customerId;

    @XmlElement
    private String email;

    @XmlElement
    private String cardPermissions; //This field has no getter and setter. I need it in order to parse correctly the JSON without the root node "customerInfo"

    @XmlElement
    private List<Account> accounts;

    public CustomerInfo() {

    }

    public CustomerInfo(String firstName, String lastName, String emailAddress) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = emailAddress;
    }

    public String getFullName() {

        return firstName + " " + lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
}

标签: javajsonjaxbunmarshallingmoxy

解决方案


你有两个选择:

  • 在注解中设置属性requiredfalse@XmlElement
  • name为所有其他字段的注释中的属性设置一个值@XmlElement,然后删除不需要的字段。通过这样做,该字段将被忽略。

有关更多和好的细节,请访问这个旧帖子


推荐阅读