Java 将类的字段设置为json转换的可选字段
我有一个spring启动应用程序。我已经公开了一个使用JSON主体的API。 有效载荷如下所示:Java 将类的字段设置为json转换的可选字段,java,json,class,jackson,field,Java,Json,Class,Jackson,Field,我有一个spring启动应用程序。我已经公开了一个使用JSON主体的API。 有效载荷如下所示: { userName: Karan, userId: 123, age: 29, addtionalInfo: {accountType: A} } class PayloadDto { private String userName; private String userId; private int age; private Map<String, String> addti
{ userName: Karan,
userId: 123,
age: 29,
addtionalInfo: {accountType: A}
}
class PayloadDto {
private String userName;
private String userId;
private int age;
private Map<String, String> addtionalInfo;
private Map<String, String> occupationDetails;
public PayloadDto(){}
//getters
//setters
}
{
"userName": "Karan",
"userId": 123,
"age": 29,
"addtionalInfo": {
"accountType": "A"
},
"occupationDetails": {
"designation": "developer",
"email": "abc@xyz.com",
"companyName": "Alfa"
}
}
我的方法有一个相应的类来处理此有效负载:
class PayloadDto {
private String userName;
private String userId;
private int age;
private Map<String, String> addtionalInfo;
public PayloadDto(){}
//getters
//setters
}
你能帮我看看我的类是什么样子的,这样它就可以很好地处理这两个json有效负载了吗
我想到了这样的写作课:
{ userName: Karan,
userId: 123,
age: 29,
addtionalInfo: {accountType: A}
}
class PayloadDto {
private String userName;
private String userId;
private int age;
private Map<String, String> addtionalInfo;
private Map<String, String> occupationDetails;
public PayloadDto(){}
//getters
//setters
}
{
"userName": "Karan",
"userId": 123,
"age": 29,
"addtionalInfo": {
"accountType": "A"
},
"occupationDetails": {
"designation": "developer",
"email": "abc@xyz.com",
"companyName": "Alfa"
}
}
class PayloadTo{
私有字符串用户名;
私有字符串用户标识;
私人互联网;
私人地图附加信息;
私人地图职业详情;
公共PayloadTo(){}
//吸气剂
//二传手
}
但这只适用于第二个json,而不适用于第一个json负载。我假设您的json实际上是有效的,如下所示:
{ userName: Karan,
userId: 123,
age: 29,
addtionalInfo: {accountType: A}
}
class PayloadDto {
private String userName;
private String userId;
private int age;
private Map<String, String> addtionalInfo;
private Map<String, String> occupationDetails;
public PayloadDto(){}
//getters
//setters
}
{
"userName": "Karan",
"userId": 123,
"age": 29,
"addtionalInfo": {
"accountType": "A"
},
"occupationDetails": {
"designation": "developer",
"email": "abc@xyz.com",
"companyName": "Alfa"
}
}
那么你的课程是:
@JsonInclude(Include.NON_EMPTY)
class PayloadDto {
private String userName;
private String userId;
private int age;
// new data types
private AdditionalInfo additionalInfo;
private OccupationDetails occupationDetails;
public PayloadDto(){}
//getters
//setters
}
因此,基本上您只需创建新类并定义其相应的成员,就像您对“基类”所做的那样
@JsonInclude(Include.NON_EMPTY)
告诉Jackson忽略缺席(您的“可选”)属性。感谢您更正我的json。我只想让我的单一API方法适用于上面提到的2种不同类型的有效负载(上面的2个JSON:一个有“occupationDetails”,而另一个没有)。所以,如果上述任何一个json负载在api调用中传递,我的方法应该可以正常工作。我的api方法如下所示:@PutMapping(value=“/v1,consumes=MediaType.APPLICATION\u JSON\u value)public String addUser(@RequestBody PayloadDataDTO payload){//对收到的有效负载做些什么}我只添加了@JsonIgnoreProperties(ignoreUnknown=true)在我的PayloadDataDTO类上。令我惊讶的是,实际上我的PayloadDataDTO类能够处理这两个有效负载。我不确定,但现在我认为即使没有任何注释,它也可以正常工作。因此,毫无疑问,我不必使用:@JsonInclude(Include.NON\u EMPTY)那么,您的问题到底是什么,您的代码现在工作了吗?使用我描述的方法,您应该能够处理这两个有效负载。additionalInfo
和occupationDetails
如果在有效负载中丢失,则将为null。嘿,我只是不知道默认情况下所有类字段都是required=false。因此,简单地添加新字段“私人职业详细信息职业详细信息;“在这个类中,对我来说很有用。我知道如何进一步使用NonNull、NonEmpty等。我只是希望这个类不要因为我提到的两个不同有效负载的调用而中断。无论如何,谢谢。”