28

I try to deserialize object that contains null-properties and have the JsonMappingException.

What I do:

String actual = "{\"@class\" : \"PersonResponse\"," +
                "  \"id\" : \"PersonResponse\"," +
                "  \"result\" : \"Ok\"," +
                "  \"message\" : \"Send new person object to the client\"," +
                "  \"person\" : {" +
                "    \"id\" : 51," +
                "    \"firstName\" : null}}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(new StringReader(json), PersonResponse.class); //EXCEPTION!

BUT: if to throw away "firstName = null" property - all works fine! I mean pass the next string:

String test = "{\"@class\" : \"PersonResponse\"," +
                "  \"id\" : \"PersonResponse\"," +
                "  \"result\" : \"Ok\"," +
                "  \"message\" : \"Send new person object to the client\"," +
                "  \"person\" : {" +
                "    \"id\" : 51}}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(new StringReader(json), PersonResponse.class); //ALL WORKS FINE!

Question: How to avoid this exception or to pledge Jackson ignore null-values during serialization?

Throws:

Message:

com.fasterxml.jackson.databind.MessageJsonException:
 com.fasterxml.jackson.databind.JsonMappingException:
  N/A (through reference chain: person.Create["person"]->Person["firstName"])

cause:

com.fasterxml.jackson.databind.MessageJsonException:
 com.fasterxml.jackson.databind.JsonMappingException:
  N/A (through reference chain: prson.Create["person"]->Person["firstName"])

cause: java.lang.NullPointerException

lambda
  • 3,295
  • 1
  • 26
  • 32
VB_
  • 45,112
  • 42
  • 145
  • 293

4 Answers4

57

Sometimes this problem occurs when accidentally using a primitive type as return type of the getter of a non-primitive field:

public class Item
{
    private Float value;

    public float getValue()
    {
        return value;
    }

    public void setValue(Float value)
    {
        this.value = value;
    }   
}

Notice the "float" instead of "Float" for the getValue()-method, this can lead to a Null Pointer Exception, even when you have added

objectMapper.setSerializationInclusion(Include.NON_NULL);
Neman
  • 1,237
  • 2
  • 13
  • 16
  • 1
    Yes. And note that this is because of implicit coercion in `getValue()`, which is basically doing "return value.floatValue()" to get primitive -- hence NPE. Caller has no way of avoiding that, or knowing there is such a problem (unless it went and decoded bytecode etc). – StaxMan Jun 04 '15 at 19:17
  • 4
    You sir saved my day :) I had a Boolean value, but accidently in the getter had it as a boolean and it was null :D – VeenarM Apr 29 '16 at 05:10
  • 1
    This was it. I also had a `Boolean` and a method `boolean get...()`. – Jelle den Burger Nov 28 '16 at 09:32
  • And my issue was that the getter / setter was had a typo. After I recreated the getters and setters using Eclipse's tools, everything works – Rami Muurimäki Aug 18 '17 at 07:47
  • you are a savior. I was debugging it and analysing like a big issue and this was a simple mistake on my side. thanks so much :) – LALIT JAMNAL Nov 14 '17 at 04:19
  • Thank you sir (another reason to use Kotlin in the future, which has a uniform type system) :) – spyro Feb 26 '18 at 13:51
  • After an hour of frustration i came upon this -- and voila! – The Head Rush May 07 '19 at 20:19
22

If you don't want to serialize null values, you can use the following setting (during serialization):

objectMapper.setSerializationInclusion(Include.NON_NULL);

Hope this solves your problem.

But the NullPointerException you get during deserialization seems suspicious to me (Jackson should ideally be able to handle null values in the serialized output). Could you post the code corresponding to the PersonResponse class?

Jackall
  • 1,120
  • 1
  • 9
  • 18
2

I also faced the same issue.

I just included a default constructor in the model class along with the other constructor with parameters.

It worked.

package objmodel;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class CarModel {

private String company;
private String model;
private String color;
private String power;


public CarModel() {
}

public CarModel(String company, String model, String color, String power) {
    this.company = company;
    this.model = model;
    this.color = color;
    this.power = power;

}

@JsonDeserialize
public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

@JsonDeserialize
public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

@JsonDeserialize
public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

@JsonDeserialize
public String getPower() {
    return power;
}

public void setPower(String power) {
    this.power = power;
}
}
0

Add JsonProperty annotation to your attribute in TO class, as below

@JsonProperty
private String id;
Shankar Murthy
  • 150
  • 2
  • 3