使用jsonschema2pojo只会创建List<Object>,没有更复杂的内容。

huangapple go评论78阅读模式
英文:

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;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"value",
"label"
})
public class BirthCountry {

@JsonProperty("value")
public Integer value;

@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;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"birthCountry",
"otherLanguages"
})
public class Example {

@JsonProperty("firstName")
public String firstName;

@JsonProperty("lastName")
public String lastName;

@JsonProperty("birthCountry")
public BirthCountry birthCountry;

@JsonProperty("otherLanguages")
public List<Object> otherLanguages = null;

}
英文:

I'm trying to use jsonschema2pojo to generate java objects, but my array of objects is just producing a List&lt;Object&gt; not a new, mapped object. Is there a config setting I need to provide?

In the below example, I was expecting an OtherLanguages POJO, and a public List&lt;Object&gt; otherLanguages; field.

example JSON:

{
    &quot;firstName&quot;: &quot;testfirst&quot;,
    &quot;lastName&quot;: &quot;testlast&quot;,
    &quot;birthCountry&quot;: {
        &quot;value&quot;: 3,
        &quot;label&quot;: &quot;Afghanistan (AF)&quot;
    },
    &quot;otherLanguages&quot;: [{
            &quot;value&quot;: 218,
            &quot;label&quot;: &quot;Uzbek (UZB)&quot;
        },
        {
            &quot;value&quot;: 216,
            &quot;label&quot;: &quot;Ukrainian (UKR)&quot;
        }
    ]
}

resulting schema (generated using https://jsonschema.net):

{
    &quot;$schema&quot;: &quot;http://json-schema.org/draft-07/schema&quot;,
    &quot;$id&quot;: &quot;http://example.com/example.json&quot;,
    &quot;type&quot;: &quot;object&quot;,
    &quot;title&quot;: &quot;The root schema&quot;,
    &quot;properties&quot;: {
        &quot;firstName&quot;: {
            &quot;$id&quot;: &quot;#/properties/firstName&quot;,
            &quot;type&quot;: &quot;string&quot;,
            &quot;title&quot;: &quot;The firstName schema&quot;
        },
        &quot;lastName&quot;: {
            &quot;$id&quot;: &quot;#/properties/lastName&quot;,
            &quot;type&quot;: &quot;string&quot;,
            &quot;title&quot;: &quot;The lastName schema&quot;
        },
        &quot;birthCountry&quot;: {
            &quot;$id&quot;: &quot;#/properties/birthCountry&quot;,
            &quot;type&quot;: &quot;object&quot;,
            &quot;title&quot;: &quot;The birthCountry schema&quot;,
            &quot;properties&quot;: {
                &quot;value&quot;: {
                    &quot;$id&quot;: &quot;#/properties/birthCountry/properties/value&quot;,
                    &quot;type&quot;: &quot;integer&quot;,
                    &quot;title&quot;: &quot;The value schema&quot;
                },
                &quot;label&quot;: {
                    &quot;$id&quot;: &quot;#/properties/birthCountry/properties/label&quot;,
                    &quot;type&quot;: &quot;string&quot;,
                    &quot;title&quot;: &quot;The label schema&quot;
                }
            },
            &quot;additionalProperties&quot;: false
        },
        &quot;otherLanguages&quot;: {
            &quot;$id&quot;: &quot;#/properties/otherLanguages&quot;,
            &quot;type&quot;: &quot;array&quot;,
            &quot;title&quot;: &quot;The otherLanguages schema&quot;,
            &quot;items&quot;: {
                &quot;$id&quot;: &quot;#/properties/otherLanguages/items&quot;,
                &quot;anyOf&quot;: [
                    {
                        &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0&quot;,
                        &quot;type&quot;: &quot;object&quot;,
                        &quot;title&quot;: &quot;The first anyOf schema&quot;,
                        &quot;properties&quot;: {
                            &quot;value&quot;: {
                                &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0/properties/value&quot;,
                                &quot;type&quot;: &quot;integer&quot;,
                                &quot;title&quot;: &quot;The value schema&quot;
                            },
                            &quot;label&quot;: {
                                &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0/properties/label&quot;,
                                &quot;type&quot;: &quot;string&quot;,
                                &quot;title&quot;: &quot;The label schema&quot;
                            }
                        },
                        &quot;additionalProperties&quot;: false
                    }
                ]
            }
        }
    },
    &quot;additionalProperties&quot;: false
}

generated POJOs (from the http://www.jsonschema2pojo.org/ site, no options selected)

-----------------------------------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
* &lt;p&gt;
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
&quot;value&quot;,
&quot;label&quot;
})
public class BirthCountry {

/**
* The value schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;value&quot;)
public Integer value;
/**
* The label schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;label&quot;)
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
* &lt;p&gt;
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
&quot;firstName&quot;,
&quot;lastName&quot;,
&quot;birthCountry&quot;,
&quot;otherLanguages&quot;
})
public class Example {

/**
* The firstName schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;firstName&quot;)
public String firstName;
/**
* The lastName schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;lastName&quot;)
public String lastName;
/**
* The birthCountry schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;birthCountry&quot;)
public BirthCountry birthCountry;
/**
* The otherLanguages schema
* &lt;p&gt;
*
*
*/
@JsonProperty(&quot;otherLanguages&quot;)
public List&lt;Object&gt; otherLanguages = null;

}

答案1

得分: 1

将创建 JSON Schema 时的数组验证更改为 First,然后它将变成一个严格的对象。

查看截图

英文:

Change Array Validation to First when creating JSON Schema, then it will become a strict object.

See the screenshot

huangapple
  • 本文由 发表于 2020年9月23日 04:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64017577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定