Use custom setter in Lombok's builder

后端 未结 4 1089
暖寄归人
暖寄归人 2021-01-01 08:42

I have a custom setter in my Lombok-based POJO:

@Data
@Builder
public class User {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncod         


        
4条回答
  •  忘掉有多难
    2021-01-01 09:22

    You are using setPassword rather than the builder's set method.

    Here is what worked for me:

    import lombok.Builder;
    import lombok.Data;
    
    @Builder
    @Data
    public class User {
        private String username;
        private String password;
    
        public static class UserBuilder {
            private String password;
            public UserBuilder password(String password ) {
                this.password ="ENCRIYP " +  password;
                return this;
            }
        }
    
        public static void main(String[] args) {
            System.out.println(User.builder().username("This is my username").password("Password").build().toString());
    
        }
    }
    

    The result was: User(username=This is my username, password=ENCRIYP Password)

提交回复
热议问题