In the Spring Boot application, we use commonly @JsonIgnore and @Transient are annotations for different purposes, particularly in data handling and persistence. We need to understand the features of @JsonIgnore vs @Transient for the better use of these annotations in the application. Jackson library provides us with @JsonIgnore annotation for controlling JSON serialization/deserialization. JPA provides us with @Transient annotation for managing database persistence.
This guide explains @JsonIgnore vs @Transient, their use cases, and when to use each in our Spring Boot applications.

What is @JsonIgnore?
@JsonIgnore is part of the Jackson Library. It is used to exclude fields from being serialized or deserialized when working with JSON.
Key Features of @JsonIgnore
- Serialization Control: Excludes fields from being included in JSON responses.
- Deserialization Control: Prevents JSON input from setting the field’s value.
- API-Level Customization: Ideal for hiding sensitive or irrelevant data in REST APIs.
Example: Using @JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
private String username;
@JsonIgnore
private String password;
// Getters and Setters
}
Usage
{
"username": "peter"
}
- The password field is excluded from the JSON response due to @JsonIgnore.
What is @Transient?
@Transient is a JPA annotation that is used on the field of Entity class when that field name column will not be created in the database table. It tells JPA to ignore the field during read and write operations.
Key Features of @Transient
- Persistence Control: Prevents a field from being stored or retrieved from the database.
- Business Logic Fields: Ideal for computed fields or temporary data used only in the application layer.
Example: Using @Transient
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class User {
@Id
private Long id;
private String username;
@Transient
private int loginAttempts;
// Getters and Setters
}
Explanation
The loginAttempts field is ignored by JPA and will not be created column with this in the database table.
Feature of @JsonIgnore Vs @Transient
Feature | @JsonIgnore | @Transient |
Purpose | Controls JSON serialization/deserialization. | Controls database persistence in JPA. |
Library | Part of Jackson (com.fasterxml.jackson.annotation). | Part of JPA (javax.persistence). |
Data Handling | Affects API-level data (JSON response/request). | Affects database-level data (persistence). |
Persistence Impact | No impact on database persistence. | Excludes the field from database operations. |
Serialization Impact | Prevents field from being serialized to JSON. | No impact on JSON serialization by default. |
Use Case | Hide sensitive or irrelevant data in API responses. | Ignore transient fields in database persistence. |
When to Use @JsonIgnore
- Sensitive Data: Hide sensitive fields like passwords or tokens in API responses.
- Irrelevant Data: Exclude fields not needed in the API layer (e.g., internal metadata).
- Custom Serialization: Use in conjunction with Jackson for precise control over JSON output.
Example Use Case
@JsonIgnore
private String creditCardNumber;
When to Use @Transient
- It is used for fields that should not be stored in the database (e.g., computed values).
- It is used when temporary fields for internal calculations or processing.
- It is used when a field does not map directly to a database column.
Example Use Case
@Transient
private double temporaryDiscount;
Using @JsonIgnore and @Transient Together
In some cases, we may need to use both these annotations @JsonIgnore and @Transient to exclude a field from both JSON serialization and database persistence.
Example: Combined Usage
@Entity
public class User {
@Id
private Long id;
private String username;
@JsonIgnore
@Transient
private String temporaryToken;
// Getters and Setters
}
Explanation
- The temporaryToken field is ignored by both JPA and Jackson.
Practical Comparison
Scenario | @JsonIgnore | @Transient |
Hide sensitive data from API | Use @JsonIgnore. | Not applicable. |
Ignore a computed field in the database | Not applicable. | Use @Transient. |
Avoid persistence and serialization | Combine @JsonIgnore and @Transient. | Combine both for full exclusion. |
Serialization only | Controls API data but persists in DB. | No control over API data. |
Best Practices
- Use @JsonIgnore for API Layer Control: Avoid exposing sensitive or irrelevant fields in API responses.
- Use @Transient for Database Control: Keep database operations clean by excluding non-persistent fields.
- Avoid Overusing Annotations: Use them judiciously to maintain clarity and maintainability.
- Combine Where Necessary: Use both annotations when a field must be excluded from both JSON and database operations.
Conclusion
The @JsonIgnore and @Transient annotations in Spring Boot serve different purposes:
- @JsonIgnore manages JSON serialization and is part of Jackson.
- @Transient controls database persistence and is part of JPA.
By understanding their differences and use cases, we can build secure, efficient, and maintainable applications in Spring Boot.