[英]Java Jackson deserialize objects of the same name but different class types
I have POJOs that are used as the request and response object in a REST API like so (I know duplicate @JsonProperty isn't syntactically correct, see below): 我有像这样的REST API中用作请求和响应对象的POJO(我知道重复的@JsonProperty在语法上不正确,请参见下文):
public class Request {
@JsonProperty("patient")
PatientObjectA patientA;
@JsonProperty("patient")
PatientObjectB patientB;
}
public class PatientObjectA {
@JsonProperty("identifier")
Private Identifier identifier
@JsonProperty("system")
Private String system;
@JsonProperty("value")
Private String value;
}
public class PatientObjectA {
@JsonProperty("identifier")
Private List<Identifier> identifier
@JsonProperty("system")
Private String system;
@JsonProperty("value")
Private String value;
}
There are minor differences in cardinality in that I want to be able to consume ie the "Patient" object will sometimes be (PatientObjectA in Request class): 在基数方面存在细微的差异,因为我希望能够使用,即有时会使用“ Patient”对象(Request类中的PatientObjectA):
"patient": {
"identifier": {
"type": {
"coding": {
"system": "NA",
"code": "Patient"
},
"text": "Patient"
},
"system": "Patient",
"value": "000000000"
}
}
or this case (note the differences in cardinality on the identifier object, where in this case identifier can have one or more items) (PatientBObject in Request class): 或这种情况(请注意,标识符对象的基数不同,在这种情况下,标识符可以包含一个或多个项目)(Request类中的PatientBObject):
"patient": {
"identifier": [{
"type": {
"coding": {
"system": "NA",
"code": "Patient"
},
"text": "Patient"
},
"system": "Patient",
"value": "3018572032"
}]
}
I would like to achieve a functionality where requests are mapped to the correct objects. 我想实现将请求映射到正确对象的功能。 Is there a way (other than a custom deserializer) where I can map the requests to the appropriate object by type/cardinality?
有没有一种方法(自定义解串器除外)可以按类型/基数将请求映射到适当的对象? Any insight would be appreciated!
任何见识将不胜感激!
Jackson support this with the @JsonTypeInfo annotation. Jackson通过@JsonTypeInfo批注支持此功能。
I recommend specifying the type info in a property (a json field) and use the full class name (as opposed to a short name) to provide a better guarantee of uniqueness: 我建议在属性(json字段)中指定类型信息,并使用完整的类名(而不是简称)来提供更好的唯一性保证:
@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectA { ..
Output A looks like: "patient": { "jsonType": "com.company.PatientAObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }
Output B looks like: "patient": { "jsonType": "com.company.PatientBObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }
输出A看起来像:
"patient": { "jsonType": "com.company.PatientAObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }
输出B看起来像: "patient": { "jsonType": "com.company.PatientBObject" "identifier": { "type": { "coding": { "system": "NA", "code": "Patient" }, "text": "Patient" }, "system": "Patient", "value": "000000000" } }
Note: Also, check out @JsonRootName, as it will give you the ability to create a 'rooted' json object without having to have that wrapper object you have. 注意:另外,请检出@JsonRootName,因为它将使您能够创建“ rooted” json对象,而不必拥有该包装对象。
@JsonRootName("Patient") @JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectA { ..
.. and .. ..和..
@JsonRootName("Patient") @JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType") public class PatientObjectB { ..
Related terms to assist with more research: 相关术语可帮助进行更多研究:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.