Tired of writing repetitive Java code? 🤔 Lombok’s here to save the day! In Spring Boot, Lombok annotations are a game-changer, cutting down boilerplate and making your code cleaner and more readable. Let’s look at the must-have Lombok annotations every Spring Boot developer should know!
1. @Getter
and @Setter
- Description: Generates getter and setter methods for all fields in a class.
-
Usage: You can apply
@Getter
and@Setter
at the class level to generate getters and setters for all fields, or at the field level to generate them only for specific fields.
@Getter @Setter public class User { private String name; private int age; }
2. @Data
-
Description: A shortcut annotation that combines
@Getter
,@Setter
,@RequiredArgsConstructor
,@ToString
, and@EqualsAndHashCode
. -
Usage: Commonly used for data transfer objects (DTOs) and entities where you need basic functionality without much customization.
@Data public class User { private String name; private int age; }
3. @AllArgsConstructor
and @NoArgsConstructor
-
Description:
@AllArgsConstructor
generates a constructor with all fields as parameters, while@NoArgsConstructor
generates a default no-argument constructor. -
Usage: Often used in combination with Spring Data JPA entities where a no-arg constructor is required, or for dependency injection when all dependencies are final.
@AllArgsConstructor @NoArgsConstructor public class User { private String name; private int age; }
4. @RequiredArgsConstructor
-
Description: Generates a constructor with parameters for all
final
fields. If used in a class with@Autowired
fields, it can be useful for dependency injection. -
Usage: Useful in Spring Boot when using constructor-based dependency injection.
@RequiredArgsConstructor public class UserService { private final UserRepository userRepository; }
5. @Builder
- Description: Implements the Builder pattern, allowing for easy and readable instantiation of objects with many parameters.
-
Usage: Helpful for creating complex objects, particularly when you don’t want to deal with constructor parameter order.
@Builder public class User { private String name; private int age; } // Usage User user = User.builder() .name("Alice") .age(25) .build();
6. @ToString
-
Description: Generates a
toString()
method. You can customize it to include or exclude specific fields. -
Usage: Often used for logging purposes.
@ToString public class User { private String name; private int age; }
7. @EqualsAndHashCode
-
Description: Generates
equals()
andhashCode()
methods, useful for comparing objects based on field values rather than references. -
Usage: Useful for entities or DTOs, especially when used in collections.
@EqualsAndHashCode public class User { private String name; private int age; }
8. @Value
-
Description: Marks a class as immutable, making all fields
private final
and removing setters. Also applies@ToString
,@EqualsAndHashCode
, and@AllArgsConstructor
. -
Usage: Commonly used for immutable data transfer objects (DTOs).
@Value public class User { String name; int age; }
9. @SneakyThrows
- Description: Allows you to throw checked exceptions without declaring them in the method signature.
-
Usage: Helpful for avoiding try-catch blocks, though should be used sparingly to ensure exception handling is explicit.
@SneakyThrows public void readFile(String path) { Files.readAllLines(Path.of(path)); }
10. @Slf4j
-
Description: Adds a
Logger
instance namedlog
to the class, making logging easier. -
Usage: Commonly used in Spring Boot applications for logging.
@Slf4j public class UserService { public void logExample() { log.info("Logging with Lombok"); } }
These annotations streamline code and reduce boilerplate, making them highly valuable in Spring Boot applications where clean, readable code is essential.