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

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

using jsonschema2pojo only creates List<Object>, nothing more complex

问题

我试图使用jsonschema2pojo来生成Java对象,但是我的对象数组只生成了一个List<Object>,而不是一个新的映射对象。是否有配置设置需要提供?

在下面的示例中,我期望一个名为OtherLanguages的POJO,以及一个public List<Object> otherLanguages字段。

示例JSON:

  1. {
  2. "firstName": "testfirst",
  3. "lastName": "testlast",
  4. "birthCountry": {
  5. "value": 3,
  6. "label": "Afghanistan (AF)"
  7. },
  8. "otherLanguages": [{
  9. "value": 218,
  10. "label": "Uzbek (UZB)"
  11. },
  12. {
  13. "value": 216,
  14. "label": "Ukrainian (UKR)"
  15. }
  16. ]
  17. }

生成的模式(使用https://jsonschema.net生成):

  1. {
  2. "$schema": "http://json-schema.org/draft-07/schema",
  3. "$id": "http://example.com/example.json",
  4. "type": "object",
  5. "title": "The root schema",
  6. "properties": {
  7. "firstName": {
  8. "$id": "#/properties/firstName",
  9. "type": "string",
  10. "title": "The firstName schema"
  11. },
  12. "lastName": {
  13. "$id": "#/properties/lastName",
  14. "type": "string",
  15. "title": "The lastName schema"
  16. },
  17. "birthCountry": {
  18. "$id": "#/properties/birthCountry",
  19. "type": "object",
  20. "title": "The birthCountry schema",
  21. "properties": {
  22. "value": {
  23. "$id": "#/properties/birthCountry/properties/value",
  24. "type": "integer",
  25. "title": "The value schema"
  26. },
  27. "label": {
  28. "$id": "#/properties/birthCountry/properties/label",
  29. "type": "string",
  30. "title": "The label schema"
  31. }
  32. },
  33. "additionalProperties": false
  34. },
  35. "otherLanguages": {
  36. "$id": "#/properties/otherLanguages",
  37. "type": "array",
  38. "title": "The otherLanguages schema",
  39. "items": {
  40. "$id": "#/properties/otherLanguages/items",
  41. "anyOf": [
  42. {
  43. "$id": "#/properties/otherLanguages/items/anyOf/0",
  44. "type": "object",
  45. "title": "The first anyOf schema",
  46. "properties": {
  47. "value": {
  48. "$id": "#/properties/otherLanguages/items/anyOf/0/properties/value",
  49. "type": "integer",
  50. "title": "The value schema"
  51. },
  52. "label": {
  53. "$id": "#/properties/otherLanguages/items/anyOf/0/properties/label",
  54. "type": "string",
  55. "title": "The label schema"
  56. }
  57. },
  58. "additionalProperties": false
  59. }
  60. ]
  61. }
  62. }
  63. },
  64. "additionalProperties": false
  65. }

生成的POJO类(从http://www.jsonschema2pojo.org/网站生成,未选择任何选项):

  1. -----------------------------------com.example.BirthCountry.java-----------------------------------
  2. package com.example;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  6. @JsonInclude(JsonInclude.Include.NON_NULL)
  7. @JsonPropertyOrder({
  8. "value",
  9. "label"
  10. })
  11. public class BirthCountry {
  12. @JsonProperty("value")
  13. public Integer value;
  14. @JsonProperty("label")
  15. public String label;
  16. }
  17. -----------------------------------com.example.Example.java-----------------------------------
  18. package com.example;
  19. import java.util.List;
  20. import com.fasterxml.jackson.annotation.JsonInclude;
  21. import com.fasterxml.jackson.annotation.JsonProperty;
  22. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  23. @JsonInclude(JsonInclude.Include.NON_NULL)
  24. @JsonPropertyOrder({
  25. "firstName",
  26. "lastName",
  27. "birthCountry",
  28. "otherLanguages"
  29. })
  30. public class Example {
  31. @JsonProperty("firstName")
  32. public String firstName;
  33. @JsonProperty("lastName")
  34. public String lastName;
  35. @JsonProperty("birthCountry")
  36. public BirthCountry birthCountry;
  37. @JsonProperty("otherLanguages")
  38. public List<Object> otherLanguages = null;
  39. }
英文:

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:

  1. {
  2. &quot;firstName&quot;: &quot;testfirst&quot;,
  3. &quot;lastName&quot;: &quot;testlast&quot;,
  4. &quot;birthCountry&quot;: {
  5. &quot;value&quot;: 3,
  6. &quot;label&quot;: &quot;Afghanistan (AF)&quot;
  7. },
  8. &quot;otherLanguages&quot;: [{
  9. &quot;value&quot;: 218,
  10. &quot;label&quot;: &quot;Uzbek (UZB)&quot;
  11. },
  12. {
  13. &quot;value&quot;: 216,
  14. &quot;label&quot;: &quot;Ukrainian (UKR)&quot;
  15. }
  16. ]
  17. }

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

  1. {
  2. &quot;$schema&quot;: &quot;http://json-schema.org/draft-07/schema&quot;,
  3. &quot;$id&quot;: &quot;http://example.com/example.json&quot;,
  4. &quot;type&quot;: &quot;object&quot;,
  5. &quot;title&quot;: &quot;The root schema&quot;,
  6. &quot;properties&quot;: {
  7. &quot;firstName&quot;: {
  8. &quot;$id&quot;: &quot;#/properties/firstName&quot;,
  9. &quot;type&quot;: &quot;string&quot;,
  10. &quot;title&quot;: &quot;The firstName schema&quot;
  11. },
  12. &quot;lastName&quot;: {
  13. &quot;$id&quot;: &quot;#/properties/lastName&quot;,
  14. &quot;type&quot;: &quot;string&quot;,
  15. &quot;title&quot;: &quot;The lastName schema&quot;
  16. },
  17. &quot;birthCountry&quot;: {
  18. &quot;$id&quot;: &quot;#/properties/birthCountry&quot;,
  19. &quot;type&quot;: &quot;object&quot;,
  20. &quot;title&quot;: &quot;The birthCountry schema&quot;,
  21. &quot;properties&quot;: {
  22. &quot;value&quot;: {
  23. &quot;$id&quot;: &quot;#/properties/birthCountry/properties/value&quot;,
  24. &quot;type&quot;: &quot;integer&quot;,
  25. &quot;title&quot;: &quot;The value schema&quot;
  26. },
  27. &quot;label&quot;: {
  28. &quot;$id&quot;: &quot;#/properties/birthCountry/properties/label&quot;,
  29. &quot;type&quot;: &quot;string&quot;,
  30. &quot;title&quot;: &quot;The label schema&quot;
  31. }
  32. },
  33. &quot;additionalProperties&quot;: false
  34. },
  35. &quot;otherLanguages&quot;: {
  36. &quot;$id&quot;: &quot;#/properties/otherLanguages&quot;,
  37. &quot;type&quot;: &quot;array&quot;,
  38. &quot;title&quot;: &quot;The otherLanguages schema&quot;,
  39. &quot;items&quot;: {
  40. &quot;$id&quot;: &quot;#/properties/otherLanguages/items&quot;,
  41. &quot;anyOf&quot;: [
  42. {
  43. &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0&quot;,
  44. &quot;type&quot;: &quot;object&quot;,
  45. &quot;title&quot;: &quot;The first anyOf schema&quot;,
  46. &quot;properties&quot;: {
  47. &quot;value&quot;: {
  48. &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0/properties/value&quot;,
  49. &quot;type&quot;: &quot;integer&quot;,
  50. &quot;title&quot;: &quot;The value schema&quot;
  51. },
  52. &quot;label&quot;: {
  53. &quot;$id&quot;: &quot;#/properties/otherLanguages/items/anyOf/0/properties/label&quot;,
  54. &quot;type&quot;: &quot;string&quot;,
  55. &quot;title&quot;: &quot;The label schema&quot;
  56. }
  57. },
  58. &quot;additionalProperties&quot;: false
  59. }
  60. ]
  61. }
  62. }
  63. },
  64. &quot;additionalProperties&quot;: false
  65. }

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

  1. -----------------------------------com.example.BirthCountry.java-----------------------------------
  2. package com.example;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  6. /**
  7. * The birthCountry schema
  8. * &lt;p&gt;
  9. *
  10. *
  11. */
  12. @JsonInclude(JsonInclude.Include.NON_NULL)
  13. @JsonPropertyOrder({
  14. &quot;value&quot;,
  15. &quot;label&quot;
  16. })
  17. public class BirthCountry {
  18. /**
  19. * The value schema
  20. * &lt;p&gt;
  21. *
  22. *
  23. */
  24. @JsonProperty(&quot;value&quot;)
  25. public Integer value;
  26. /**
  27. * The label schema
  28. * &lt;p&gt;
  29. *
  30. *
  31. */
  32. @JsonProperty(&quot;label&quot;)
  33. public String label;
  34. }
  35. -----------------------------------com.example.Example.java-----------------------------------
  36. package com.example;
  37. import java.util.List;
  38. import com.fasterxml.jackson.annotation.JsonInclude;
  39. import com.fasterxml.jackson.annotation.JsonProperty;
  40. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  41. /**
  42. * The root schema
  43. * &lt;p&gt;
  44. *
  45. *
  46. */
  47. @JsonInclude(JsonInclude.Include.NON_NULL)
  48. @JsonPropertyOrder({
  49. &quot;firstName&quot;,
  50. &quot;lastName&quot;,
  51. &quot;birthCountry&quot;,
  52. &quot;otherLanguages&quot;
  53. })
  54. public class Example {
  55. /**
  56. * The firstName schema
  57. * &lt;p&gt;
  58. *
  59. *
  60. */
  61. @JsonProperty(&quot;firstName&quot;)
  62. public String firstName;
  63. /**
  64. * The lastName schema
  65. * &lt;p&gt;
  66. *
  67. *
  68. */
  69. @JsonProperty(&quot;lastName&quot;)
  70. public String lastName;
  71. /**
  72. * The birthCountry schema
  73. * &lt;p&gt;
  74. *
  75. *
  76. */
  77. @JsonProperty(&quot;birthCountry&quot;)
  78. public BirthCountry birthCountry;
  79. /**
  80. * The otherLanguages schema
  81. * &lt;p&gt;
  82. *
  83. *
  84. */
  85. @JsonProperty(&quot;otherLanguages&quot;)
  86. public List&lt;Object&gt; otherLanguages = null;
  87. }

答案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:

确定