如果属性名称不等于字段名称,则Jackson @JsonProperty无法正常工作 [英] Jackson @JsonProperty not working if property name not equal field name

查看:184
本文介绍了如果属性名称不等于字段名称,则Jackson @JsonProperty无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON

{
  "known-name": "Zevs",
  "approximate-age": 320
}

和绑定类

public class GodBinding {

  @JsonProperty("known-name")
  public String name;

  @JsonProperty("approximate-age")
  public int age;

  // constructors
  // getters & setters
}

和后续的Maven依赖关系2.23.22.5.4

And followng maven dependencies 2.23.2 2.5.4

 <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>
 </dependencies>

如果我发布这样的json,那么我会得到带有空值的意外结果.

If I post such json then I have unexpected result with null's.

GodBinding [name=null, age=0]

如果我使用不带名称的@JsonProperty并在属性名称等于字段名称的情况下发送JSON

If I use @JsonProperty without names and send JSON where property names equal field names

{
  "name": "Zevs",
  "age": 320
}

然后一切正常

GodBinding [name=Zevs, age=320]

如果有人知道,如何使@JsonProperty("name")在正常工作的字段上?

If somebody know, how to make @JsonProperty("name") on fields working correctly?

推荐答案

这通常是由于杰克逊1的注释是杰克逊1而引起的,但是您要使用杰克逊2,就像其他许多问题中提到的那样.

This is often caused when the annotations of Jackson is of Jackson 1, but you want to use Jackson 2, as mentioned in many other questions.

在我的情况下,在项目中,我还有另一个依赖关系,该依赖关系被错误地导入:

In my case, in the project I have another dependency which was imported wrongly:

import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

当我创建 ObjectMapper 时.我想 testcontainers 使用其自己的 ObjectMapper 作为其依赖项,并错误地公开了它;实际上它是一个较旧的版本.不确定是哪个.

when I create the ObjectMapper. I guess testcontainers uses its own ObjectMapper as its dependency and exposed it incorrectly; actually it is an older version. Not sure which is.

我将其更改为

import com.fasterxml.jackson.databind.ObjectMapper;

,现在一切正常.这就是我所说的第一级依赖关系",而不是依赖关系的依赖关系".在我的gradle文件中,它是2.3版.

and all works now. This is what I call "first level dependency", not "dependency of dependency". In my gradle file it is version 2.3.

我之所以这样说是因为

  • 我看到其他问题只是提到Jackson 1和2之间的困惑,而不是 testcontainer 的困惑.我们只必须忽略那些不是 fasterxml.jackson 的代码.

  • I see other questions only mentioning the confusion between Jackson 1 and 2, not this of testcontainer. We just must ignore those not of fasterxml.jackson.

不仅要注意 @JsonProperty 等版本,,还要注意导入 ObjectMapper 反序列化功能.

Pay attention of the version not only of @JsonProperty, etc, but also the version of Jackson you use when importing ObjectMapper and DeserializationFeature.

这篇关于如果属性名称不等于字段名称,则Jackson @JsonProperty无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆