1

I got a User class, which look like this :

public class User implements Serializable {

    @JsonProperty("image")
    private UserImage image;

    @JsonProperty("email")
    private String email;

    @JsonProperty("mobilePhone")
    private String mobilePhone;

    @JsonProperty("firstname")
    private String firstName;

    @JsonProperty("lastname")
    private String lastName;

    @JsonProperty("codeCreatedAt")
    private String codeCreatedAt;

    @JsonProperty("accountSettings")
    private AccountSettings accountSettings;

    @JsonProperty("status")
    private String status;

    @JsonProperty("created")
    private String created;

    @JsonProperty("id")
    private String id;

    public User(String lastName, String firstName, String mobilePhone) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.mobilePhone = mobilePhone;
    }

    public User() {
    }

    public User(UserImage image, String email, String mobilePhone, String firstName, String lastName, String codeCreatedAt, AccountSettings accountSettings, String status, String created, String id) {
        this.image = image;
        this.email = email;
        this.mobilePhone = mobilePhone;
        this.firstName = firstName;
        this.lastName = lastName;
        this.codeCreatedAt = codeCreatedAt;
        this.accountSettings = accountSettings;
        this.status = status;
        this.created = created;
        this.id = id;
    }
}

I am trying to send a Json like: {"mobilePhone":"123456789","lastname":"test","firstname":"test"}

I create a new user , using the constructor with 3 parameters, but it makes JSON based on constructor with all parameters. What I need to change to obtain the desired JSON ?

Alex Mccrorie
  • 21
  • 1
  • 8
test
  • 13
  • 7

1 Answers1

2

add @JsonInclude(JsonInclude.Include.NON_NULL) to the class

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {...}
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58