java @JsonIgnore vs @Transient -difference?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29762328/
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
@JsonIgnore vs @Transient -difference?
提问by Abhishek Kumar
Which one to use for skipping field for serialization and de-serialization.
哪个用于跳过字段以进行序列化和反序列化。
@JsonIgnorewhy we should use it if @Transient also skips the field from serialization and de-serialization process?
@JsonIgnore如果@Transient 也跳过序列化和反序列化过程中的字段,为什么我们应该使用它?
回答by shazin
The clear difference between the two is that @Transientis used as part of JPA to ignore a field from persisting if it is marked as @Transient.
两者之间的明显区别在于,@Transient它用作 JPA 的一部分以忽略标记为@Transient.
Where as @JsonIgnoreis only used to Ignore a marked field from being serialized, de-serialized to and from JSON.
其中 as@JsonIgnore仅用于忽略标记的字段被序列化,反序列化到 JSON 和从 JSON 反序列化。
Which means a field marked as @JsonIgnorecan still be persisted in a JPA persistence where as a field marked @Transientwill neither be persisted nor be serialized, de-serialized.
这意味着标记为的字段@JsonIgnore仍然可以在 JPA 持久化中持久化,而标记为的字段@Transient既不会被持久化,也不会被序列化、反序列化。
回答by moraleboost
We should distinguish between javax.persistence.Transientand java.beans.Transient. As mentioned by @shazin and @Abhishek Kumar, the former signals JPA to ignore the property for persistence and does not affect marshalling. Hymanson treats the latter the same as JsonIgnoreduring marshalling, as can be seen in HymansonAnnotationIntrospector#_isIgnorable(Annotated):
我们应该区分javax.persistence.Transient和java.beans.Transient。正如@shazin 和@Abhishek Kumar 所提到的,前者向 JPA 发出信号以忽略持久性的属性并且不影响编组。Hymanson 对待后者的方式与JsonIgnore编组期间相同,如下所示HymansonAnnotationIntrospector#_isIgnorable(Annotated):
protected boolean _isIgnorable(Annotated a)
{
JsonIgnore ann = _findAnnotation(a, JsonIgnore.class);
if (ann != null) {
return ann.value();
}
if (_java7Helper != null) {
Boolean b = _java7Helper.findTransient(a);
if (b != null) {
return b.booleanValue();
}
}
return false;
}
where Java7SupportImpl#findTransient(Annotated)is looking for java.beans.Transient.
在哪里Java7SupportImpl#findTransient(Annotated)找java.beans.Transient。
回答by nabster
@Transientis an allegory to transientkeyword in Java language. When used with a variable, it is never serialized.
@Transient是transientJava语言中关键字的寓言。当与变量一起使用时,它永远不会被序列化。
Example:
例子:
public class Person {
String name;
int age;
String password;
public Person(String name, int age, String password) {
this.name = name;
this.age = age;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Transient
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public void serializationTest() throws JsonProcessingException {
Person aPerson = new Person("Demonte", 37, "bestKeptSecret1995");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(aPerson);
System.out.println(json);
}
By annotating getPassword()(remember getters for serialization)@Transientwill produce
通过注释getPassword()(记住序列化的getter)@Transient将产生
{"name":"Demonte","age":37}
Now if you revisit the Personclass code and remove @Transientand add transientto the passwordvariable and also add a feature to the Hymanson mapper to tell it how to handle fields marked as transient
现在,如果您重新访问Person类代码并删除@Transient并添加transient到password变量,并向 Hymanson 映射器添加一个功能来告诉它如何处理标记为的字段transient
transient String password;
and
和
mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
(remember Hymanson uses getters not members directly for serialization)then you will get the same output.
(请记住 Hymanson 使用 getter 而不是成员直接进行序列化)然后您将获得相同的输出。

