Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonIgnore vs @Transient -difference?

Tags:

Which one to use for skipping field for serialization and de-serialization.

@JsonIgnore why we should use it if @Transient also skips the field from serialization and de-serialization process?

like image 235
Abhishek Kumar Avatar asked Apr 21 '15 03:04

Abhishek Kumar


People also ask

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.

What is @JsonIgnore in spring boot?

The. @JsonIgnore. annotation marks a field in a POJO to be ignored by Jackson during serialization and deserialization. Jackson ignores the field in both JSON serialization and deserialization.

When should I use JsonIgnore?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property.

What is Jsonproperty order?

The @JsonPropertyOrder is an annotation to be used at the class-level. It takes as property a list of fields that defines the order in which fields can appear in the string resulting from the object JSON serialization.


1 Answers

The clear difference between the two is that @Transient is used as part of JPA to ignore a field from persisting if it is marked as @Transient.

Where as @JsonIgnore is only used to Ignore a marked field from being serialized, de-serialized to and from JSON.

Which means a field marked as @JsonIgnore can still be persisted in a JPA persistence where as a field marked @Transient will neither be persisted nor be serialized, de-serialized.

like image 170
shazin Avatar answered Oct 29 '22 21:10

shazin