问题描述
JsonProperty
不会覆盖杰克逊从吸气剂获得的默认名称.如果我用ObjectMapper
和jackson序列化下面的类,我会得到
JsonProperty
isn't overriding the default name jackson gets from the getter. If I serialize the class below with ObjectMapper
and jackson I get
{"hi":"hello"}
如您所见,JsonProperty批注无效
As you can see the JsonProperty annotation has no effect
class JacksonTester {
String hi;
@JsonProperty("hello")
public String getHi() {
return hi;
}
}
在字符串本身上放置@JsonProperty
也不起作用.似乎我可以更改名称的唯一方法是通过重命名getter,唯一的问题是,对于第一个字母,该名称将始终为小写
Putting @JsonProperty
on the String itself doesn't work either. The only way it seems that I can change the name is by renaming the getter, the only problem is that it then will always be lowercase for the first letter
推荐答案
问题是我同时使用了旧的和新的杰克逊库
The problem was that I was using both the old and new jackson libraries
即在我之前import org.codehaus.jackson.annotate.JsonProperty;
为了与我使用的库保持一致,我必须将其更改为以下内容.
i.e. before I hadimport org.codehaus.jackson.annotate.JsonProperty;
Which I had to change to below, to be consistent with the library I was using.
自从我使用Maven以来,这还意味着更新我的Maven依赖项.import com.fasterxml.jackson.annotation.JsonProperty;
Since I was using maven that also meant updating my maven dependencies.import com.fasterxml.jackson.annotation.JsonProperty;
要使其正常工作,我需要在吸气剂上添加@JsonProperty
注释(将其放在对象上无效)
For it to work, I needed the @JsonProperty
annotation on the getter (putting it on the object didn't work)
我在这里找到了答案(多亏了francescoforesti) @JsonProperty无法按预期工作
I found the answer here (thanks to francescoforesti)@JsonProperty not working as expected
这篇关于杰克逊不使用@JsonProperty覆盖吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!