package com.example.demo.model;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.List;
// Complete data class with all common methods
@Data
public class Product {
private Long id;
private String name;
private String description;
private double price;
private int stock;
private LocalDateTime createdAt;
}
// Builder pattern
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order {
private Long id;
private Long userId;
private List<OrderItem> items;
@Builder.Default
private String status = "PENDING";
@Builder.Default
private LocalDateTime createdAt = LocalDateTime.now();
}
// Immutable value object
@Value
public class Money {
double amount;
String currency;
public Money add(Money other) {
if (!currency.equals(other.currency)) {
throw new IllegalArgumentException("Currency mismatch");
}
return new Money(amount + other.amount, currency);
}
}
// With method for immutable updates
@Value
@With
public class UserSettings {
String theme;
String language;
boolean notifications;
}
// Required args constructor for dependency injection
@RequiredArgsConstructor
@Slf4j
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
public Order processOrder(Order order) {
log.info("Processing order: {}", order.getId());
try {
paymentService.charge(order);
order.setStatus("PAID");
log.debug("Payment successful for order: {}", order.getId());
} catch (Exception e) {
log.error("Payment failed for order: {}", order.getId(), e);
order.setStatus("FAILED");
}
return orderRepository.save(order);
}
}
// Getter only (read-only)
@Getter
@AllArgsConstructor
public class ReadOnlyModel {
private final Long id;
private final String name;
}
// Custom toString
@ToString(exclude = "password")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {
@EqualsAndHashCode.Include
private Long id;
private String username;
private String password;
private String email;
}
// Cleanup resources automatically
public class ResourceExample {
public void processFile(String path) {
@Cleanup val inputStream = new FileInputStream(path);
// Stream automatically closed
}
}
// Synchronized methods
public class ThreadSafeCounter {
private int count = 0;
@Synchronized
public void increment() {
count++;
}
@Synchronized
public int getCount() {
return count;
}
}
Project Lombok generates common Java code via annotations at compile-time. @Data creates getters, setters, toString, equals, and hashCode. @Builder implements the builder pattern. @Slf4j provides logger instances. @NoArgsConstructor and @AllArgsConstructor generate constructors. @RequiredArgsConstructor creates constructors for final fields. @Value makes immutable classes. @With creates copies with modified fields. Lombok reduces hundreds of lines of boilerplate, improving maintainability. IDE plugins enable code navigation. Configuration via lombok.config customizes behavior. While powerful, I use Lombok judiciously—overuse can obscure code behavior. The tool integrates seamlessly with Spring Boot, making DTOs and entities concise. Lombok enhances developer productivity without runtime dependencies.
Related snips
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
GraphQL API with Spring Boot
package com.example.starter.config;
import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Custom Spring Boot starters
package com.example.demo.config;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
Messaging with Apache Kafka
package com.example.demo.controller;
import com.example.demo.dto.FileMetadata;
import com.example.demo.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
File upload and download handling
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Database migration with Flyway
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
WebSocket for real-time communication
Share this code
Here's the card — post it anywhere.