简体   繁体   English

从 Java 对象生成 JSON Schema

[英]Generate JSON Schema from Java object

I need to generate JSON schema from Java Class, I am using Jackson Mapper to generate the same.我需要从 Java 类生成 JSON 模式,我正在使用 Jackson Mapper 生成相同的模式。 below is java code -下面是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);
}

and my I have two entity classes one is Employee.java -我有两个实体类,一个是 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;
    }
}

and the other is Profile.java另一个是 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;
    }
}

And the generated JSON Schema is -生成的 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" } } } } } }

this is getting generated by my application, but i need the schema with "required" true or false.这是由我的应用程序生成的,但我需要带有“必需”true 或 false 的架构。 so is there any way out we can achieve this either by some annotations or any other thing.那么有什么办法可以通过一些注释或任何其他方式来实现这一点。 the desired format which i want looks like bit similar to this -我想要的所需格式看起来与此有点相似-

 { "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"] } } } }

Please suggest me if it is possible.如果可能,请建议我。

try to use mbknor-jackson-jsonschema https://github.com/mbknor/mbknor-jackson-jsonSchema尝试使用 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM