【问题标题】:Jackson ObjectMapper not able to recognize @JsonProperty AnnotationJackson ObjectMapper 无法识别@JsonProperty 注解
【发布时间】: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。

标签: java json maven jackson2


【解决方案1】:

我在我的代码中发现了问题。 我曾使用 JaxbAnnotationIntrospector,它利用适用于 JSON 映射的 JAXB 注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多