2

The json string is like this:

[
   {
     "id":1545043316459,
     "user_id":127118102570729472,
     "username":"abcd",
     "phone":"12413453563",
     "password":"b6b33aad5c337315d4ac5a34d82db990cfaf22e5eefe27220b6827d5e66ad8b4",
     "license":"12351331",
     "plate":"abcdef",
     "avatar":"",
     "approval":0,
     "balance":0.0
   }
]

When I use ObjectMapper.convertValue() to convert this json to a List<User>, the method returns null. However, when I changes the field "approval" to 1, the convertion succeeds.
Th class User's constructor is as below:

@JsonCreator
public User(@JsonProperty("user_id") long userId, @JsonProperty("username") String username, @JsonProperty("password") String password, @JsonProperty("avatar") String avatar,
                @JsonProperty("phone") String phone, @JsonProperty("license") String license, @JsonProperty("plate") String plate, @JsonProperty("approval") int approval,
                @JsonProperty("balance") BigDecimal balance) {
   this.userId = userId;
   this.username = username;
   this.password = password;
   this.avatar = avatar;
   this.phone = phone;
   this.license = license;
   this.plate = plate;
   this.balance = balance;
   this.approval = approval;
}

I have applied @JsonIgnoreProperties(ignoreUnknown = true) to the class.

Lesterth
  • 77
  • 1
  • 6
  • Possible duplicate of [Jackson serialization: Ignore uninitialised int](https://stackoverflow.com/questions/33372004/jackson-serialization-ignore-uninitialised-int) – Dherik Dec 17 '18 at 11:22

1 Answers1

3

Jackson ignores primitive types with default value.

To fix this you can :

  1. Put annotation on field approval

    @JsonInclude(Include.NON_DEFAULT)
    
  2. Change type from int to Integer

Solution is up to you

Dherik
  • 17,757
  • 11
  • 115
  • 164
mslowiak
  • 1,688
  • 12
  • 25