[英]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.