【发布时间】:2019-05-15 10:39:16
【问题描述】:
我正在使用 jsonschema2pojo-maven-plugin 版本 0.4.30 创建 POJO。但是当我在我的代码中使用这些 pojo 时,Jackson ObjectMapper 无法识别 @JsonProperty 注释。以下是示例 json:
{
"title": "IP Address",
"description": "Ip Address",
"type": "object",
"properties": {
"ip_address": {
"type": "string",
"minLength": 1,
"maxLength": 39,
"description": "ip address"
}
}
}
我已尝试匹配 jackson-databind 版本,但没有成功。
@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({"ip_address"})
public class IpGeo {
@JsonProperty("ip_address")
@JsonPropertyDescription("ip address")
@Size(
min = 1,
max = 39
)
private String ipAddress;
@JsonProperty("ip_address")
public String getIpAddress() {
return this.ipAddress;
}
@JsonProperty("ip_address")
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
我希望 ObjectMapper 从 json 创建 IpGeo 类。它应该将 ip_address 映射到 ipAddress。但它给出了一个错误“ip_address field not recongnized”。
【问题讨论】:
-
请提供如何反序列化 json 的代码。
-
ipGeo = jsonMapper .readValue(new String(response.getValue()), IpGeo.class);
-
在您的 json 示例中,我看不到字符串
ip_address字段。只是对象ip_address。 -
ip_address 在 properties: 下,即字段 ip_address。