简体   繁体   English

@JsonProperty对字段的注释以及getter / setter

[英]@JsonProperty annotation on field as well as getter/setter

I have inherited a certain bit code that has the @JsonProperty annotation on getter/setters. 我继承了一些在getter / setter上有@JsonProperty注释的位代码。 The purpose is so that when the object is serialized using the Jackson library, the fields have that specific name. 目的是当使用Jackson库序列化对象时,字段具有该特定名称。

Current code: 当前代码:

private String fileName;

@JsonProperty("FILENAME")
public String getFileName()
{
    return fileName;
}

@JsonProperty("FILENAME")
public void setFileName(String fileName)
{
    this.fileName = fileName;
}

Now for another tool, I need to annotate the field with JsonProperty as well. 现在换另一个工具,我需要用JsonProperty来注释该字段。 So this will be my changed code: 所以这将是我改变的代码:

@JsonProperty("FILENAME")
private String fileName;

@JsonProperty("FILENAME")
public String getFileName()
{
    return fileName;
}

@JsonProperty("FILENAME")
public void setFileName(String fileName)
{
    this.fileName = fileName;
}

Has anyone used this same annotation on both - the field as well as the getter/setters? 有没有人在这两个领域使用相同的注释 - 领域以及吸气剂/安装者? I looked around on the net but didn't see anything. 我环顾四周,但没有看到任何东西。

I have compiled & run the code but I'm not sure if this would this cause any problems down the road. 我已编译并运行代码,但我不确定这是否会导致任何问题。 Any thoughts on this? 有什么想法吗?

My observations based on a few tests has been that whichever name differs from the property name is one which takes effect: 我基于一些测试的观察结果是,与属性名称不同的名称是生效的名称:

For eg. 例如。 consider a slight modification of your case: 考虑稍微修改一下你的案子:

@JsonProperty("fileName")
private String fileName;

@JsonProperty("fileName")
public String getFileName()
{
    return fileName;
}

@JsonProperty("fileName1")
public void setFileName(String fileName)
{
    this.fileName = fileName;
}

Both fileName field, and method getFileName , have the correct property name of fileName and setFileName has a different one fileName1 , in this case Jackson will look for a fileName1 attribute in json at the point of deserialization and will create a attribute called fileName1 at the point of serialization. fileName字段和方法getFileName都具有正确的fileName属性名称, setFileName具有不同的fileName1 ,在这种情况下,Jackson将在反序列化时在json中查找fileName1属性,并在该点创建名为fileName1的属性序列化。

Now, coming to your case, where all the three @JsonProperty differ from the default propertyname of fileName , it would just pick one of them as the attribute( FILENAME ), and had any on of the three differed, it would have thrown an exception: 现在,来到你的情况,所有三个@JsonProperty都与fileName的默认属性名不同,它只选择其中一个作为属性( FILENAME ),并且三个不同的任何一个,它会抛出一个异常:

java.lang.IllegalStateException: Conflicting property name definitions

In addition to existing good answers, note that Jackson 1.9 improved handling by adding "property unification", meaning that ALL annotations from difference parts of a logical property are combined, using (hopefully) intuitive precedence. 除了现有的好答案之外,请注意Jackson 1.9通过添加“属性统一”来改进处理,这意味着使用(希望)直观优先级来合并来自逻辑属性的不同部分的所有注释。

In Jackson 1.8 and prior, only field and getter annotations were used when determining what and how to serialize (writing JSON); 在Jackson 1.8和之前的版本中,在确定序列化的内容和编写方式时,只使用了字段和getter注释(编写JSON); and only and setter annotations for deserialization (reading JSON). 并且仅用于反序列化的setter注释(读取JSON)。 This sometimes required addition of "extra" annotations, like annotating both getter and setter. 这有时需要添加“额外”注释,例如注释getter和setter。

With Jackson 1.9 and above these extra annotations are NOT needed. 使用Jackson 1.9及以上版本时,不需要这些额外的注释。 It is still possible to add those; 仍然可以添加那些; and if different names are used, one can create "split" properties (serializing using one name, deserializing using other): this is occasionally useful for sort of renaming. 如果使用不同的名称,可以创建“拆分”属性(使用一个名称进行序列化,使用其他名称进行反序列化):这有时可用于重命名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM