[英]Json schema with anyOf field with same name and different types to POJO + Jackson
If have a schema with component A that contains AnyOf section with two items.如果有一个包含组件 A 的模式,其中包含带有两个项目的 AnyOf 部分。 The difference between them is that in one case child component C is array and in another one C is object but they have the same name B. Is it possible to handle it with jackson if I have two java objects for that?它们之间的区别在于,在一种情况下,子组件 C 是数组,而在另一种情况下,子组件 C 是 object 但它们具有相同的名称 B。如果我有两个 java 对象,是否可以使用 jackson 处理它?
I'm thinking if I can use interface with some annotations and jackson will determine the correct object...我在想我是否可以使用带有一些注释的接口,jackson 将确定正确的 object...
"A": {
"type": "object",
"anyOf": [
{
"properties": {
"B": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/components/schemas/C"
}
}
},
"additionalProperties": false
},
{
"properties": {
"B": {
"type": "object",
"$ref": "#/components/schemas/C"
}
},
"additionalProperties": false
}
]
}
Lets suppose I have this in java假设我在 java 中有这个
public class AAnyOf1 {
@JsonProperty("B")
private List<C> b = new ArrayList<>();
...
}
public class AAnyOf2 {
@JsonProperty("B")
private C b = null;
...
}
This is very popular pattern to send in response a JSON Object
instead of JSON Array
with one JSON Object
.这是一种非常流行的模式,可以发送一个JSON Object
而不是JSON Array
和一个JSON Object
。 So, instead of:所以,而不是:
{
"b": [
{
"id": 1
}
]
}
API
response looks like: API
响应如下所示:
{
"b": {
"id": 1
}
}
Jackson
already handle this use case. Jackson
已经处理了这个用例。 You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature and you need only one version of POJO
:您需要启用ACCEPT_SINGLE_VALUE_AS_ARRAY功能并且您只需要一个版本的POJO
:
class AAnyOf {
@JsonProperty("B")
private List<C> b;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.