我有一个像这样的模型:
现在我需要使用这个(唯一的)模型创建两个JSON。
第一个JSON应该像这样,例如:
第二个示例必须像这样,例如:
顺便说一下,我已经测试过
public class Employee {
@JsonProperty("emplyee_id")
private Integer id;
@JsonProperty("emplyee_first_name")
private String firstName;
@JsonProperty("emplyee_last_name")
private String lastName;
@JsonProperty("emplyee_address")
private String address;
@JsonProperty("emplyee_age")
private Byte age;
@JsonProperty("emplyee_level")
private Byte level;
//getters and setters
}
现在我需要使用这个(唯一的)模型创建两个JSON。
第一个JSON应该像这样,例如:
{
"employee_id":101,
"employee_first_name":"Alex",
"employee_last_name":"Light",
"employee_age":null,
"employee_address":null
}
第二个示例必须像这样,例如:
{
"employee_id":101,
"employee_level":5
}
顺便说一下,我已经测试过
@JsonIgnore
和@JsonInclude(JsonInclude.Include.NON_NULL)
。第一个的问题(据我所知)是这些字段不能包含在其他JSON中(例如如果level
获取此注释,则不会包含在第二个JSON中)。第二个的问题是,null
值不能包含在JSON中。那么我能保留null值并防止某些其他属性被包含在JSON中,而无需创建额外的模型吗?如果答案是肯定的,那么该怎么做呢?如果不是,我真的很感谢任何人给我最好的解决方案。非常感谢。