Getting JsonMappingException while sending data to view

Jackson, by default, serializes an object's whole inheritance hierarchy, ie. the parent class fields as well. In the case of

public class BinderResponse extends WebApiResponseBase {

it seems like

Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"])

Jackson tries to serialize a field called valid from a getter called isValid (which is a conventional bean property name). The getter method, however, seems to throw a NullPointerException for whatever reason.

If you want Jackson to ignore it, you can annotate the getter with @JsonIgnore or your class with @JsonIgnoreProperties and specify the property name, ie. valid.


In my case when I used @JsonIgnore the exception has been gone but the problem was it couldn't receive that value from API Request anymore and Spring ignored it (obviously because of @JsonIgnore) So I investigated about the issue and figured out that the problem was the getter and setter. I had the Integer property while my getter was int. So when I changed the getter to Integer my problem solved and error's gone.

private Integer purchaseId;

@JsonIgnore
public int getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(int purchaseId) {
    this.purchaseId = purchaseId;
}

Changed to :

private Integer purchaseId;


public Integer getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(Integer purchaseId) {
    this.purchaseId = purchaseId;
}