简体   繁体   中英

Generate JSON Schema from Java object

I need to generate JSON schema from Java Class, I am using Jackson Mapper to generate the same. below is java code -

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 -

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

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 -

 { "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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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