jsonchema2pojo:如何将 minLength、maxLength 范围应用于生成的 Java Pojo class?

jsonchema2pojo: How the minLength, maxLength bounds applied on generated Java Pojo class?

我想知道 JSON 模式中提供的边界如何转换为 java POJO class?

例如下面是definitions.json文件内容-

{
"definitions": {

    "address": {
        "type": "object",
        "properties": {
            "street": { "type": "string", "minLength" : 30, "maxLength" : 100 },
            "city": { "type": "string", "minLength" : 30, "maxLength" : 100 },
            "state": { "type": "string", "minLength" : 30, "maxLength" : 100  }
        }
}

}
}

使用 jsonschema2pojo 实用程序将上述 json 模式转换为 pojo class,如下所示 -

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "street",
    "city",
    "state"
})
public class Address {

    @JsonProperty("street")
    private String street;
    @JsonProperty("city")
    private String city;
    @JsonProperty("state")
    private String state;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The street
     */
    @JsonProperty("street")
    public String getStreet() {
        return street;
    }

    /**
     * 
     * @param street
     *     The street
     */
    @JsonProperty("street")
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * 
     * @return
     *     The city
     */
    @JsonProperty("city")
    public String getCity() {
        return city;
    }

    /**
     * 
     * @param city
     *     The city
     */
    @JsonProperty("city")
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * 
     * @return
     *     The state
     */
    @JsonProperty("state")
    public String getState() {
        return state;
    }

    /**
     * 
     * @param state
     *     The state
     */
    @JsonProperty("state")
    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @Override
    public int hashCode() {
        ..... Generated implementation
    }

    @Override
    public boolean equals(Object other) {
        .....Generated Implementation
    }

}

现在,我们在 pojo class 的任何地方都看不到 minLength 和 maxLength 边界的实现。

我的问题是,在转换为 java pojo 时是否忽略了边界?有什么方法可以使这些界限计入 pojo class?

此外,JSON 模式定义可以有模式来验证节点的值,例如 -

    {
    "type": "object",
    "properties": {
        "name": {
        "type": "string",
        "pattern": "^[xX]?[0-9a-fA-F]{32}$"
        },
        "address": { "$ref": "definations.json#/definitions/address" }

    }
}

转换为 java pojo class 时模式是否被忽略?有什么方法可以将这些模式纳入 Java POJO class?

minLength/maxLength的值应该用双引号引起来。

默认值参见documentation中提供的示例: { "type":"integer", "default":"100"}

对于模式案例,请检查允许通过配置参数激活 JSR-303 注释的 Maven 插件、CLI 和 Ant 任务的可用性。

When activated, the following JSR-303 annotations will be generated:

Schema rule Annotation maximum @DecimalMax minimum @DecimalMin minItems,maxItems @Size minLength,maxLength @Size pattern @Pattern ...