Java Jackson 不使用 @JsonProperty 覆盖 Getter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32843099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson Not Overriding Getter with @JsonProperty
提问by tt_Gantz
JsonProperty
isn't overriding the default name Hymanson gets from the getter. If I serialize the class below with ObjectMapper
and Hymanson I get
JsonProperty
没有覆盖 Hymanson 从 getter 获得的默认名称。如果我用ObjectMapper
Hymanson序列化下面的类,我会得到
{"hi":"hello"}
As you can see the JsonProperty annotation has no effect
可以看到 JsonProperty 注解没有效果
class HymansonTester {
String hi;
@JsonProperty("hello")
public String getHi() {
return hi;
}
}
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
穿上@JsonProperty
String 本身也不起作用。似乎我可以更改名称的唯一方法是重命名 getter,唯一的问题是它的第一个字母总是小写
采纳答案by tt_Gantz
The problem was that I was using both the old and new Hymanson libraries
问题是我同时使用了旧的和新的 Hymanson 库
i.e. before I had
import org.codehaus.Hymanson.annotate.JsonProperty;
Which I had to change to below, to be consistent with the library I was using.
即之前我必须
import org.codehaus.Hymanson.annotate.JsonProperty;
更改为下面,以与我正在使用的库保持一致。
Since I was using maven that also meant updating my maven dependencies.
import com.fasterxml.Hymanson.annotation.JsonProperty;
由于我使用的是 maven,这也意味着更新我的 maven 依赖项。
import com.fasterxml.Hymanson.annotation.JsonProperty;
For it to work, I needed the @JsonProperty
annotation on the getter (putting it on the object didn't work)
为了让它起作用,我需要在@JsonProperty
getter 上添加注释(将它放在对象上不起作用)
I found the answer here (thanks to francescoforesti) @JsonProperty not working as expected
我在这里找到了答案(感谢 francescoforesti) @JsonProperty 没有按预期工作
回答by mprivat
Place it on the variable, not the getter
将它放在变量上,而不是 getter
class HymansonTester {
@JsonProperty("hello")
private String hi;
public String getHi() {
return hi;
}
}
回答by vvs
Have you tried below
你有没有试过下面
class HymansonTester {
private String hi;
@JsonProperty("hello")
public String getHi() {
return hi;
}
}
I mean making the 'hi' variable declaration as private. Alternatively try to put a @JsonIgnore on the variable declaration and in case you would rather keep it at default scope.
我的意思是将 'hi' 变量声明设为私有。或者尝试将 @JsonIgnore 放在变量声明上,以防您宁愿将其保留在默认范围内。
回答by gpushkas
Camel cases still seem to have issues even after defining proper annotations. Example:
即使在定义了适当的注释之后,Camel 案例似乎仍然存在问题。例子:
@JsonProperty("mirrorport") private String mirrorPort;
@JsonProperty("mirrorport") private String mirrorPort;
Deserialization still fails when xml has <mirrorport>YES</mirrorport>
当 xml 有时,反序列化仍然失败 <mirrorport>YES</mirrorport>
回答by Dmitriy S
I had same proplem
我有同样的问题
You need just to replace import import com.fasterxml.Hymanson.annotation.JsonProperty; on import org.codehaus.Hymanson.annotate.JsonProperty; Its work.
你只需要替换 import import com.fasterxml.Hymanson.annotation.JsonProperty; 导入 org.codehaus.Hymanson.annotate.JsonProperty;这是工作。
回答by Michael Kolakowski
I recently came across another interesting twist on this issue. We started using the Hibernate5Module to help with some lazy loading issues. Furthermore, we use Groovy so we are not defining getters and setters.
我最近在这个问题上遇到了另一个有趣的转折。我们开始使用 Hibernate5Module 来帮助解决一些延迟加载问题。此外,我们使用 Groovy,因此我们没有定义 getter 和 setter。
It turns out that the Hibernate Module seems to interfere with the @JsonProperty
annotation. Specifically if you have something annotated with @Transient
So, if you have something like:
事实证明,Hibernate 模块似乎干扰了@JsonProperty
注释。特别是如果你有一些用@Transient
So注释的东西,如果你有这样的东西:
@Transient
@ApiModelProperty(required = true)
@JsonProperty("alternateName")
String property
You won't see the alternateName
in the JSON. Furthermore, your clients will likely have trouble with their POSTs and PUTs! To fix this, you can use a simple workaround. Define the getters and setters for the internal name you need to use(*) and don't use the value
attribute on @JsonProperty
So this works:
您不会alternateName
在 JSON 中看到。此外,您的客户可能会在处理他们的 POST 和 PUT 时遇到问题!要解决此问题,您可以使用一个简单的解决方法。为您需要使用的内部名称定义 getter 和 setter(*) 并且不要使用value
属性 on@JsonProperty
所以这有效:
@Transient
@ApiModelProperty(required = true)
@JsonProperty
String alternateName
void setProperty(String property) {
this.alternateName = property
}
@JsonIgnore
String getProperty() {
this.alternateName
}
Note the use of the @JsonIgnore
on the getter. If you don't, your framework will probably pick it up and you'll have duplicate entries for the same thing in your JSON.
请注意@JsonIgnore
getter 上的使用。如果不这样做,您的框架可能会选择它,并且您的 JSON 中将有相同内容的重复条目。
Anyhow - I'm hoping this helps someone!
无论如何 - 我希望这对某人有所帮助!
(*)We were trying to adhere to a particular interface, thus enforcing the name internally. However, the exposed API needed a different, user-friendly name.
(*) 我们试图坚持一个特定的接口,从而在内部强制执行这个名称。但是,公开的 API 需要一个不同的、用户友好的名称。
回答by Xchai
I had this problem when updating from older version to 2.8.3 of FasterXML Hymanson.
从旧版本更新到 FasterXML Hymanson 的 2.8.3 时,我遇到了这个问题。
The issue was when deserializing the JSON response from our DB into Java class object, our code didn't have @JsonSetter
on the class' setters. Hence, when serializing, the output wasn't utilizing the class' getters to serialize the Java class object into JSON (hence the @JsonProperty()
decorator wasn't taking effect).
问题是当将来自我们的 DB 的 JSON 响应反序列化为 Java 类对象时,我们的代码@JsonSetter
在类的 setter 中没有。因此,在序列化时,输出没有利用类的 getter 将 Java 类对象序列化为 JSON(因此@JsonProperty()
装饰器没有生效)。
I fixed the issue by adding @JsonSetter("name-from-db")
to the setter method for that property.
我通过添加@JsonSetter("name-from-db")
到该属性的 setter 方法来解决这个问题。
Also, instead of @JsonProperty()
, to rename properties using the getter method, you can and should use @JsonGetter()
which is more specific to renaming properties.
此外,@JsonProperty()
您可以并且应该使用@JsonGetter()
更具体的重命名属性来代替, 使用 getter 方法重命名属性。
Here's our code:
这是我们的代码:
public class KwdGroup {
private String kwdGroupType;
// Return "type" instead of "kwd-group-type" in REST API response
@JsonGetter("type") // Can use @JsonProperty("type") as well
public String getKwdGroupType() {
return kwdTypeMap.get(kwdGroupType);
}
@JsonSetter("kwd-group-type") // "kwd-group-type" is what JSON from the DB API outputs for code to consume
public void setKwdGroupType(String kwdGroupType) {
this.kwdGroupType = kwdGroupType;
}
}
回答by ketankk
I was missing databind dependency
我缺少数据绑定依赖项
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>${fasterxml.version}</version>
</dependency>
回答by Khaled
I know its an old question but for me I got it working when I figured out that its conflicting with Gson library so I had to use @SerializedName("name")
instead of @JsonProperty("name")
hope this helps
我知道这是一个老问题,但对我来说,当我发现它与 Gson 库冲突时,我得到了它的工作,所以我不得不使用@SerializedName("name")
而不是@JsonProperty("name")
希望这有帮助
回答by Francisco C.
If using Kotlin
如果使用 Kotlin
I understand the original question is in Java, but since Kotlin is becoming very popular and many might be using it I'd like to post this here to help others.
我知道最初的问题是在 Java 中,但是由于 Kotlin 变得非常流行并且许多人可能正在使用它,我想在这里发布它以帮助其他人。
Anyway, for Kotlin, because how getters/setters work, if you are using val
, meaning you only expose the getter, you may need to apply the annotation to the getter like below:
无论如何,对于 Kotlin,由于 getter/setter 的工作方式,如果您使用的是val
,这意味着您只公开 getter,您可能需要将注释应用到 getter,如下所示:
class HymansonTester(@get:JsonProperty("hello") val hi: String)