1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Data @ApiModel(description = "User Model") @NoArgsConstructor @AllArgsConstructor @Builder public class User { @NotEmpty(message = "email can not be empty") private String email; @NotEmpty(message = "password can not be empty") private String password; } |
Ref: javabydeveloper.com (All the content!)
1 |
@Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: |
1 2 3 4 5 6 7 8 9 |
getter methods for all fields, setter methods for all non-final fields, appropriate toString(), appropriate equals() and hashCode() implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull, in order to ensure the field is never null. In the above code, two approaches are equal. @Dataannotation minimizes the usage of more annotations when you need to generate all the above 7 requirements, they are most common for the Pojo classes. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
--> @Setter(AccessLevel.PROTECTED) private String name; ### protected void setName(String name) { this.name = name; } <-- --> @Getter @Setter private int age = 10; ### public int getAge() { return age; } /** * Age of the person. Water is wet. * * @param age New value for this person's age. Sky is blue. */ public void setAge(int age) { this.age = age; } <-- ############### |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Lombok @Data ignore/exclude fields @Data annotation alone not provide support for ignoring fields from generating getters/setters or toString or equals and hashCode methods. Following example demonstrates how to exclude fields when you are using @Data annotation. @Data public class User4 { @Setter(value = AccessLevel.NONE) private Long id; @Getter(value = AccessLevel.NONE) private String username; @ToString.Exclude // !!! private boolean active; @EqualsAndHashCode.Exclude /// !! @ToString.Exclude /// !! private int role; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
6. Lombok @Data and @Builder together If you use @Data annotation alone, public required-args constructor is generated. If you are using @Data and @Builder annotations together, all-args constructor (Package access level) is generated. In case initializing objects is responsible for third party libraries like Spring Framework, most of the cases they look for no-args constructor. Following example demonstrates how you can generate all-args and no-args constructor when you are using @Data and @Builder together. @AllArgsConstructor(access = AccessLevel.PACKAGE) @NoArgsConstructor @Data @Builder public class User5 { private Long id; private String username; } |
Delombok
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Usage # Note that Lombok requires Java 6, but the plugin requires Java 7, or later, to execute at build time. # Delombok # Add the following to the pom.xml to activate the delombok goal: <build> <plugins> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.18.20.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> </plugin> </plugins> </build> # Place the java source code with lombok annotations -- in src/main/lombok (instead of src/main/java). -- During the build process, the src/main/lombok -- code will be delomboked and the generated java code ends up -- in target/generated-sources/delombok. -- The delomboked code is compiled and analysed together with -- the src/main/java code. |
–
–
–
–