【问题标题】:Generate JSON Schema from Java object从 Java 对象生成 JSON Schema
【发布时间】:2021-03-11 02:08:42
【问题描述】:

我需要从 Java 类生成 JSON 模式,我正在使用 Jackson Mapper 生成相同的模式。 下面是java代码 -

private static String getJsonSchema(Class clazz) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);

    JsonSchema schema = mapper.generateJsonSchema(clazz);

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
}

我有两个实体类,一个是 Employee.java -

class Employee
{
    private Long id;
    private List<Profile> profiles;

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the profiles
     */

    public List<Profile> getProfiles() {
        return profiles;
    }

    /**
     * @param profiles the profiles to set
     */
    public void setProfiles(List<Profile> profiles) {
        this.profiles = profiles;
    }
}

另一个是Profile.java

public class Profile
{
    private Integer id;
    private String name;
    private String address;
    private String value;

    /**
     * @return the name
    */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
}

而生成的 JSON Schema 是 -

{
  "type" : "object",
  "properties" : {
    "id" : {
      "type" : "number"
    },
    "profiles" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "id" : {
            "type" : "integer"
          },
          "name" : {
            "type" : "string"
          },
          "address" : {
            "type" : "string"
          },
          "value" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

这是由我的应用程序生成的,但我需要具有“必需”真或假的模式。那么有什么方法可以通过一些注释或任何其他方式来实现这一点。 我想要的所需格式看起来有点类似于这个 -

    {
  "type" : "object",
  "properties" : {
    "id" : {
      "type" : "number"
    },
    "profiles" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "id" : {
            "type" : "integer"
          },
          "name" : {
            "type" : "string"
          },
          "address" : {
            "type" : "string"
          },
          "value" : {
            "type" : "string"
          }
        },
      "required": ["id", "name", "address"]
      }
    }
  }
}

如果可能,请给我建议。

【问题讨论】:

  • 这三个成员标记为required 在您的Java 类中的什么位置?
  • 您能否建议我如何将任何字段标记为必填项?我以相反的方式尝试,我采用所需数组的 json 模式并生成了 java 类,但我没有看到 java 类有任何区别。就像我在没有所需数组的情况下生成一样。

标签: java json


【解决方案1】:

尝试使用 mbknor-jackson-jsonschema https://github.com/mbknor/mbknor-jackson-jsonSchema

// A standard validation @NotNull annotation.
@NotNull
public String foo;

// Using the Jackson @JsonProperty annotation, specifying the attribute as required.
@JsonProperty(required = true)
public String bar;

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2015-03-07
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多