JAXB(MOXy)-解组null值

时间:2018-10-02 10:23:55

标签: java json jaxb unmarshalling moxy

我在JSon与JAXB MOXy解组时遇到麻烦。

以下是我要解析的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;
    }
}

1 个答案:

答案 0 :(得分:0)

您有两种选择:

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

有关更多详细信息,请转到旧的post