gpt4 book ai didi

java - jsonchema2pojo : How the minLength, maxLength 边界应用于生成的 Java Pojo 类?

转载 作者:行者123 更新时间:2023-12-01 23:28:21 27 4
gpt4 key购买 nike

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

例如,下面是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 类,如下 -

@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 类中任何地方实现了 minLength 和 maxLength 边界。

我的问题是,转换为 java pojo 时边界是否被忽略?有什么方法可以将这些界限纳入到 pojo 类中吗?

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

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

}
}

转换为 java pojo 类时模式是否会被忽略?有什么方法可以将这些模式计入 Java POJO 类中吗?

最佳答案

minLength/maxLength 的值应使用双引号。

请参阅 documentation 中提供的示例对于默认值: {“类型”:“整数”,“默认”:“100”}

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

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

Schema rule Annotationmaximum @DecimalMaxminimum @DecimalMinminItems,maxItems @SizeminLength,maxLength @Sizepattern @Pattern...

关于java - jsonchema2pojo : How the minLength, maxLength 边界应用于生成的 Java Pojo 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58299664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com