@Data is a Lombok class annotation that keeps Java domain objects compact by generating common methods at compile time. It supplies getters, setters, equals(), hashCode(), toString(), and a constructor for final properties. section 2.1.1
A compact domain class
Annotating Ingredient with @Data provides the usual accessors and utility methods for its final id, name, and type fields without writing them manually. The same pattern defines Taco and TacoOrder objects; TacoOrder can still add its own addTaco method. section 2.1.1
Add Lombok to the project before using @Data:
- Declare org.projectlombok:lombok as a project dependency. section 2.1.1
- Exclude Lombok from the Spring Boot Maven plugin because its code generation happens at compile time, not runtime. section 2.1.1
Listing 2.1 Defining taco ingredients
package tacos;
import lombok.Data;
@Data
public class Ingredient {
private final String id;
private final String name;
private final Type type;
public enum Type {
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}
View the complete Ingredient class showing the import, @Data annotation, fields, and nested enum. section 2.1.1
