简体   繁体   English

Jackson ObjectMapper 无法识别@JsonProperty 注释

[英]Jackson ObjectMapper not able to recognize @JsonProperty Annotation

I am creating POJO's using jsonschema2pojo-maven-plugin, version 0.4.30.我正在使用版本 0.4.30 的 jsonschema2pojo-maven-plugin 创建 POJO。 But when I use those pojo's in my code, Jackson ObjectMapper is not able to recognise @JsonProperty annotation.但是当我在我的代码中使用这些 pojo 时,Jackson ObjectMapper 无法识别 @JsonProperty 注释。 Below is the sample json:下面是样本 json:

{
  "title": "IP Address",
    "description": "Ip Address",
    "type": "object",
    "properties": {
         "ip_address": {
             "type": "string",
             "minLength": 1,
             "maxLength": 39,
             "description": "ip address"
           }
    }
}

I have tried matching the jackson-databind versions, but it didn't worked.我试过匹配 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;
    }
}

I expect ObjectMapper creates IpGeo class from the json. It should map ip_address to ipAddress.我希望 ObjectMapper 从 json 创建 IpGeo class。它应该是 map ip_address 到 ipAddress。 But it gives an error "ip_address field not recongnized".但它给出了一个错误“ip_address field not recongnized”。

I found the issue in my code. 我在代码中发现了这个问题。 I had use JaxbAnnotationIntrospector that leverages JAXB annotations where applicable to JSON mapping. 我使用了JaxbAnnotationIntrospector,它利用适用于JSON映射的JAXB注释。

In my case, I had the same issue because I created an instance of ObjectMapper on my own instead of auto-wiring it.就我而言,我遇到了同样的问题,因为我自己创建了一个 ObjectMapper 实例,而不是自动连接它。 Here is the line in Kotlin:这是 Kotlin 中的行:

@Autowired
lateinit var objectMapper: ObjectMapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jackson @JsonProperty注释可以递归工作吗? - Jackson @JsonProperty annotation works recursivly? 杰克逊在反序列化中忽略了JsonProperty注释 - Jackson ignores JsonProperty annotation in deserialization 对 kotlin 数据类使用 Jackson @JsonProperty 注释 - Usage of Jackson @JsonProperty annotation for kotlin data classes Jackson 对象映射器如何忽略 JsonProperty 注释? - Jackson object mapper how to ignore JsonProperty annotation? Jackson ObjectMapper 忽略所有没有注释的属性 - Jackson ObjectMapper ignore all properties that has no annotation Jackson ObjectMapper 无法将字符串解析为哈希图 - Jackson ObjectMapper not able to parse string as hashmap 杰克逊·杰森房产 - Jackson JsonProperty 在 Jackson 中使用 ObjectMapper 时验证(required=true)在 JsonProperty 中设置应该抛出异常 - Validating (required=true) set in JsonProperty while using ObjectMapper in Jackson should throw Exception 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 杰克逊ObjectMapper设置JsonFormat.Shape.ARRAY不带注释 - Jackson ObjectMapper set JsonFormat.Shape.ARRAY without annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM