问题
I have something like this:
@RequiredArgsConstructor
public class Person {
private String name;
private LocalDate date = null;
}
I need a constructor for the name attribute only, but it is not working. If I use @NonNull it will build the constructor I need, but for my case the name should be able to be null, can not be final either. Any ideas? Thanks!
回答1:
Lombok @RequiredArgsConstructor has no such option currently.
You have at least two options:
- Just code the constructor
public Person(String name)without help of lombok annotations. Use inheritance - which might be a bit overkill solution in your case but might be handy in some cases - as presented below:
Create a base class that holds all the fields you do not want to be in
Personconstructor:public abstract class BasePerson { private LocalDate date = null; }Let your
Personclass extend above with the fields you need in constructor but want to benullalso, with@AllArgsConstructor:@AllArgsConstructor public abstract class Person extends BasePerson { private String name; }
来源:https://stackoverflow.com/questions/48790494/use-lombok-requiredargsconstructor-without-nonnull