Java / Lombok Interview questions
How do you exclude a field from @ToString?
Annotating a specific field with @ToString.Exclude removes just that field from the generated
toString() output, while every other field is still included as usual.
@ToString public class User { private String username; @ToString.Exclude private String password; } // toString() produces "User(username=ada)" - password is omitted
This is the standard way to keep sensitive fields (passwords, tokens, secrets) out of log output, since
toString() is frequently what ends up in application logs, whether directly logged or captured
incidentally through an object being passed to a logging call. It's good practice to apply this proactively to
any field holding sensitive data, rather than discovering the leak after it's already appeared in production
logs.
More Related questions...