0
votes

I'm having troubles with JSon unmarshalling with JAXB MOXy.

Below is the JSON I want to parse:

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

The problem here is with the field "cardPermissions".

I'm trying to unmarshal an object which doesn't have "cardPermissions" field (no @XmlElement annotation and class attribute).

JAXB throws a NullPointerException while unmarshalling this string because it is:

"cardPermissions": null

and not:

"cardPermissions": "null"

To accept the null value, I had to add inside my POJO the field cardPermissions (with @XmlElement annotation).

Doing this, also without getters and setters, JAXB can unmarshall the provided JSON correctly.

null and "null" are completely different things but I do not want to include this field inside my POJO, and I have to ignore these null values.

EDIT

If i include the root element ("customerInfo") like this:

{
    "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 gets parsed correctly, but i do not want to include root when i unmarshall the json.

This is my 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

1 Answers

0
votes

You have two choices :

  • Set the attribute required to false in the annotation @XmlElement
  • Set a value to the attribute name in the annotation @XmlElement of all the other fields and then delete the unwanted field. By doing this, the field will be ignored.

For more and good details go to this old post.