从Java对象生成JSON模式

时间:2015-01-29 15:10:59

标签: java json

我需要从Java Class生成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"
          }
        }
      }
    }
  }
}

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

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

如果有可能,请建议我。

1 个答案:

答案 0 :(得分:0)

尝试使用 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;