This question already has answers here:
How to tell Jackson to ignore a field during serialization if its value is null?
(18个回答)
3年前关闭。
我有一个
我正在尝试发送Json,例如:
我使用具有3个参数的构造函数创建了一个新用户,但它基于具有所有参数的构造函数生成JSON。我需要更改以获得所需的JSON吗?
(18个回答)
3年前关闭。
我有一个
User
类,看起来像这样: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;
}
}
我正在尝试发送Json,例如:
{"mobilePhone":"123456789","lastname":"test","firstname":"test"}
我使用具有3个参数的构造函数创建了一个新用户,但它基于具有所有参数的构造函数生成JSON。我需要更改以获得所需的JSON吗?
最佳答案
将@JsonInclude(JsonInclude.Include.NON_NULL)
添加到类中
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {...}
10-06 09:39