Jan Nielsen :
I have a custom setter in my Lombok-based POJO:
@Data
@Builder
public class User {
private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
private String password = null;
public void setPassword(String password) {
Assert.notNull(password);
this.password = ENCODER.encode(password);
}
but when I use the Lombok generated builder:
User user = User.builder()
.password(password)
.build();
my custom setter is not invoked, and so the password is not encoded. This makes me sad.
My custom setter is, of course, invoked when I use it directly:
public void changePassword(String password, User user) {
user.setPassword(password);
}
What can I do to have Lombok's builder use my custom setter?
chrylis -on strike- :
Per the documentation for @Builder
: Just define enough skeleton yourself. In particular, Lombok will generate a class UserBuilder
, fields mirroring the User
fields, and builder methods, and you can provide any or all of this yourself.
@Builder
public class User {
private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
private String username;
private String password;
public static class UserBuilder {
public UserBuilder password(String password) {
this.password = ENCODER.encode(password);
return this;
}
}
}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=446148&siteId=1