xiaohongshu
xiaohongshu
lombok-patterns
Lombok annotations and best practices for Java 21+ projects. Use when reducing boilerplate, configuring builders, or choosing between Lombok and Records.
Full skill instructions
This skill provides guidance on using Project Lombok to reduce boilerplate code in Java applications, covering annotations, best practices, and integration patterns.
Note: This guide targets Java 21+ projects. Some Lombok features are now obsolete due to modern Java features (Records,
var, try-with-resources). See Java 21+ Considerations below.
Lombok is a Java library that automatically generates common code like getters, setters, constructors, builders, and more during compilation. It helps create cleaner, more maintainable code.
Some Lombok features are now redundant with modern Java:
| Lombok Feature | Java Alternative | Recommendation |
|---|---|---|
val/var | var (Java 10+) | Use Java var |
@Value | record (Java 16+) | Prefer records for simple value objects |
@Data | record (Java 16+) | Prefer records for simple DTOs |
@Cleanup | try-with-resources (Java 7+) | Use try-with-resources |
@With | record wither methods | Records can define wither methods |
@Builder: Records don't have built-in builder support@Slf4j: No Java equivalent for logger generation@RequiredArgsConstructor: Useful for Spring dependency injection| Annotation | Purpose | Use Case |
|---|---|---|
@Getter/@Setter | Accessor methods | JPA entities, mutable classes |
@NoArgsConstructor | No-argument constructor | JPA entities, serialization |
@AllArgsConstructor | All-fields constructor | Dependency injection |
@RequiredArgsConstructor | Constructor for final/@NonNull fields | Spring services |
@Builder | Builder pattern implementation | Complex object construction |
| Lombok | Use Java Record When |
|---|---|
@Value | Simple immutable data carriers |
@Data | Simple DTOs without inheritance |
// ✅ Prefer Java Record for simple value objects
public record Money(BigDecimal amount, String currency) {}
// ✅ Prefer Java Record for simple DTOs
public record CustomerDTO(String name, String email) {}
// ✅ Use Lombok for JPA entities (records don't work with JPA)
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Customer {
@Id private Long id;
private String name;
}
| Annotation | Logger Type |
|---|---|
@Slf4j | SLF4J (recommended) |
@Log4j2 | Log4j 2 |
@Log | java.util.logging |
@CommonsLog | Apache Commons Logging |
| Annotation | Purpose |
|---|---|
@NonNull | Null checks on parameters/fields |
@With | Immutable setters (creates new instance) |
@SneakyThrows | Throw checked exceptions without declaration |
@Synchronized | Thread-safe method synchronization |
@ToString | Customizable toString() generation |
@EqualsAndHashCode | Customizable equals/hashCode |
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Product {
@Id
@EqualsAndHashCode.Include
private Long id;
private String name;
private BigDecimal price;
}
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
public Order process(Order order) {
log.info("Processing order: {}", order.getId());
return orderRepository.save(order);
}
}
// Records don't have builder support - use Lombok
@Builder
public record Email(
String to,
String subject,
String body,
@Singular List<String> attachments
) {}
// Usage
Email email = Email.builder()
.to("[email protected]")
.subject("Hello")
.body("Content")
.attachment("file1.pdf")
.build();
@RequiredArgsConstructor with final fields for dependency injection@Slf4j for logging instead of manual logger creation@Builder for classes with many optional fields@EqualsAndHashCode on JPA entities@Data or @Value when a Java Record would suffice@Data on JPA entities (use individual annotations)@EqualsAndHashCode with lazy-loaded JPA relationships@SneakyThrows (makes exception handling unclear)| Pitfall | Problem | Solution |
|---|---|---|
@Data on entities | Includes mutable setters, problematic equals/hashCode | Use @Getter, @Setter, explicit @EqualsAndHashCode |
Missing @NoArgsConstructor | JPA/Jackson need no-arg constructor | Always add with @AllArgsConstructor |
Circular @ToString | StackOverflow with bidirectional relationships | Use @ToString.Exclude on one side |
| Using Lombok for simple DTOs | Adds unnecessary dependency | Use Java Records instead |
@Data (consider Records instead)@Value (consider Records instead)@Getter and @Setter@Builder@NonNull@Withxiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience