使用 jsonschema2pojo 只创建 List<Object>,没有更复杂的
using jsonschema2pojo only creates List<Object>, nothing more complex
我正在尝试使用 jsonschema2pojo 生成 java 个对象,但我的对象数组只是生成一个 List<Object>
而不是新的映射对象。我需要提供配置设置吗?
在下面的示例中,我期待一个 OtherLanguages
POJO 和一个 public List<Object> otherLanguages;
字段。
示例JSON:
{
"firstName": "testfirst",
"lastName": "testlast",
"birthCountry": {
"value": 3,
"label": "Afghanistan (AF)"
},
"otherLanguages": [{
"value": 218,
"label": "Uzbek (UZB)"
},
{
"value": 216,
"label": "Ukrainian (UKR)"
}
]
}
结果架构(使用 https://jsonschema.net 生成):
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"properties": {
"firstName": {
"$id": "#/properties/firstName",
"type": "string",
"title": "The firstName schema"
},
"lastName": {
"$id": "#/properties/lastName",
"type": "string",
"title": "The lastName schema"
},
"birthCountry": {
"$id": "#/properties/birthCountry",
"type": "object",
"title": "The birthCountry schema",
"properties": {
"value": {
"$id": "#/properties/birthCountry/properties/value",
"type": "integer",
"title": "The value schema"
},
"label": {
"$id": "#/properties/birthCountry/properties/label",
"type": "string",
"title": "The label schema"
}
},
"additionalProperties": false
},
"otherLanguages": {
"$id": "#/properties/otherLanguages",
"type": "array",
"title": "The otherLanguages schema",
"items": {
"$id": "#/properties/otherLanguages/items",
"anyOf": [
{
"$id": "#/properties/otherLanguages/items/anyOf/0",
"type": "object",
"title": "The first anyOf schema",
"properties": {
"value": {
"$id": "#/properties/otherLanguages/items/anyOf/0/properties/value",
"type": "integer",
"title": "The value schema"
},
"label": {
"$id": "#/properties/otherLanguages/items/anyOf/0/properties/label",
"type": "string",
"title": "The label schema"
}
},
"additionalProperties": false
}
]
}
}
},
"additionalProperties": false
}
生成的 POJO(来自 http://www.jsonschema2pojo.org/ 站点,未选择任何选项)
-----------------------------------com.example.BirthCountry.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* The birthCountry schema
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"value",
"label"
})
public class BirthCountry {
/**
* The value schema
* <p>
*
*
*/
@JsonProperty("value")
public Integer value;
/**
* The label schema
* <p>
*
*
*/
@JsonProperty("label")
public String label;
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* The root schema
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"birthCountry",
"otherLanguages"
})
public class Example {
/**
* The firstName schema
* <p>
*
*
*/
@JsonProperty("firstName")
public String firstName;
/**
* The lastName schema
* <p>
*
*
*/
@JsonProperty("lastName")
public String lastName;
/**
* The birthCountry schema
* <p>
*
*
*/
@JsonProperty("birthCountry")
public BirthCountry birthCountry;
/**
* The otherLanguages schema
* <p>
*
*
*/
@JsonProperty("otherLanguages")
public List<Object> otherLanguages = null;
}
创建JSON模式时将数组验证更改为First
,然后它将成为一个严格的对象。
See the screenshot
我正在尝试使用 jsonschema2pojo 生成 java 个对象,但我的对象数组只是生成一个 List<Object>
而不是新的映射对象。我需要提供配置设置吗?
在下面的示例中,我期待一个 OtherLanguages
POJO 和一个 public List<Object> otherLanguages;
字段。
示例JSON:
{
"firstName": "testfirst",
"lastName": "testlast",
"birthCountry": {
"value": 3,
"label": "Afghanistan (AF)"
},
"otherLanguages": [{
"value": 218,
"label": "Uzbek (UZB)"
},
{
"value": 216,
"label": "Ukrainian (UKR)"
}
]
}
结果架构(使用 https://jsonschema.net 生成):
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"properties": {
"firstName": {
"$id": "#/properties/firstName",
"type": "string",
"title": "The firstName schema"
},
"lastName": {
"$id": "#/properties/lastName",
"type": "string",
"title": "The lastName schema"
},
"birthCountry": {
"$id": "#/properties/birthCountry",
"type": "object",
"title": "The birthCountry schema",
"properties": {
"value": {
"$id": "#/properties/birthCountry/properties/value",
"type": "integer",
"title": "The value schema"
},
"label": {
"$id": "#/properties/birthCountry/properties/label",
"type": "string",
"title": "The label schema"
}
},
"additionalProperties": false
},
"otherLanguages": {
"$id": "#/properties/otherLanguages",
"type": "array",
"title": "The otherLanguages schema",
"items": {
"$id": "#/properties/otherLanguages/items",
"anyOf": [
{
"$id": "#/properties/otherLanguages/items/anyOf/0",
"type": "object",
"title": "The first anyOf schema",
"properties": {
"value": {
"$id": "#/properties/otherLanguages/items/anyOf/0/properties/value",
"type": "integer",
"title": "The value schema"
},
"label": {
"$id": "#/properties/otherLanguages/items/anyOf/0/properties/label",
"type": "string",
"title": "The label schema"
}
},
"additionalProperties": false
}
]
}
}
},
"additionalProperties": false
}
生成的 POJO(来自 http://www.jsonschema2pojo.org/ 站点,未选择任何选项)
-----------------------------------com.example.BirthCountry.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* The birthCountry schema
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"value",
"label"
})
public class BirthCountry {
/**
* The value schema
* <p>
*
*
*/
@JsonProperty("value")
public Integer value;
/**
* The label schema
* <p>
*
*
*/
@JsonProperty("label")
public String label;
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* The root schema
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"birthCountry",
"otherLanguages"
})
public class Example {
/**
* The firstName schema
* <p>
*
*
*/
@JsonProperty("firstName")
public String firstName;
/**
* The lastName schema
* <p>
*
*
*/
@JsonProperty("lastName")
public String lastName;
/**
* The birthCountry schema
* <p>
*
*
*/
@JsonProperty("birthCountry")
public BirthCountry birthCountry;
/**
* The otherLanguages schema
* <p>
*
*
*/
@JsonProperty("otherLanguages")
public List<Object> otherLanguages = null;
}
创建JSON模式时将数组验证更改为First
,然后它将成为一个严格的对象。
See the screenshot