I'm adding this answer since Sumeet's is outdated as of 2019.

You can provide your own getter or setter logic by simply writing your own methods.

package com.example;

import lombok.Getter;

@Getter
public class Foo {

    //Will not generate a getter for 'test'
    private String test;

    //Will generate a getter for 'num' because we haven't provided our own
    private int num;

    public String getTest(){
        if(test == null){
            throw new Exception("Don't mind me, I'm just a silly exception");
        }

        return test;
    }
}
Answer from rath on Stack Overflow
🌐
Project Lombok
projectlombok.org › features › GetterSetter
@Getter and @Setter
You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean). A default setter is named setFoo if the field is called foo, returns void, and takes 1 parameter of the same type as the field.
🌐
CodeJava
codejava.net › coding › java-getter-and-setter-tutorial-from-basics-to-best-practices
Java Getter and Setter Tutorial - from Basics to Best Practices
September 30, 2019 - For the setter, add cloned items from source collection to the destination one. For the getter, create a new collection which is being returned. Add cloned items from the original collection to the new one. If you define a custom type of object, you should implement clone() method for your own type.
Top answer
1 of 6
136

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;
        }
    }
}
2 of 6
20

I've accepted chrylis's answer but for completeness, here's are some ways to minimize customization and duplication.

Custom setter and builder with static helper

A static helper can be used to share most of the set password functionality across the custom User.UserBuilder::password method and the custom User::setPassword method:

@Data
@Builder
public class User {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private String password = null;

    public void setPassword(String password) {
        this.password = _encodePassword(password);
    }
    
    public static class UserBuilder {
        public UserBuilder password(String password) {
            this.password = _encodePassword(password)
            return this;
        }
    }

    private static String _encodePassword(String password) {
        Assert.notNull(password);
        return ENCODER.encode(password);
    }
}

Custom setter and constructor

A custom constructor can use User::setPassword which is invoked by the Lombok generated User.UserBuilder::build():

@Data
@Builder
public class User {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private String password = null;

    User(String password) {
        setPassword(password);
    }

    public void setPassword(String password) {
        Assert.notNull(password);
        this.password = ENCODER.encode(password);
    }
}

Custom setter and constructor with static helper

Or, a little more elegantly, with a custom constructor and a static helper method:

@Data
@Builder
public class User {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private String password = null;

    User(String password) {
        _encodePassword(password, this);
    }

    public void setPassword(String password) {
        _encodePassword(password, this);
    }

    private static _encodePassword(String password, User user) {
        Assert.notNull(password);
        user.password = ENCODER.encode(password);
    }
}
🌐
DZone
dzone.com › data engineering › data › java getter and setter: basics, common mistakes, and best practices
Best Practices for Java Getter and Setter - DZone
October 1, 2019 - For the setter, add cloned items from the source Collection to the destination one. For the getter, create a new Collection, which is being returned. Add cloned items from the original Collection to the new one.
🌐
Baeldung
baeldung.com › home › java › lombok builder with custom setter
Lombok Builder with Custom Setter | Baeldung
February 19, 2025 - @Data generates all the boilerplate that is normally associated with a simple POJO (Plain Old Java Object): getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode implementations, and a constructor.
🌐
Rustam Rakhimov
rusyasoft.github.io › java, lombok › 2020 › 02 › 14 › Overriding-build-setter-lombok-builder
Override build and setter in Lombok's builder | Rustam Rakhimov
February 14, 2020 - @Builder @Getter public class Person { private String name; private String address; /** * Uses custom builder class */ public static PersonBuilder builder() { return new CustomPersonBuilder(); } /** * Cusotm builder class */ private static class CustomPersonBuilder extends PersonBuilder { @Override public Person build() { // Validates required fields Validate.notBlank(super.name, "Persons NAME cannot be null or empty!"); return super.build(); } } }
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-lombok-builder-custom-setter
Lombok Builder Custom Setter: An In-Depth Guide - CodingTechRoom
Override the default setters in the generated Builder class to include custom logic such as validation or logging.
🌐
Medium
medium.com › @sdtapusd20 › customizing-getters-setters-generation-in-intellij-idea-80c2d2ca07d9
Customizing Getters & Setters Generation in IntelliJ IDEA | by Tapu Sutradhar | Medium
October 8, 2023 - Follow the steps to save custom Getters and Setters template: 1. Right click on A Java class file editor 2. Select Generate 3. Choose Getter and Setter 4. In the Method Selection Pop Up Choose ... 3 dot symbol beside getter or setter Template Selection 5. A new template management pop up will appear 6.
🌐
Dozermapper
dozermapper.github.io › gitbook › documentation › custommethods.html
Custom get() set() Methods · GitBook
For those beans that might have unorthodox getter and setter methods, Dozer support user specified setter and getter methods. To make a bi-directional mapping in this case, look at the following example below. The source field in element A specifies a custom setter method and getter method using attributes.
🌐
JetBrains
jetbrains.com › help › idea › generating-code.html
Generate code | IntelliJ IDEA Documentation
August 17, 2026 - Select the fields to generate getters or setters for and click OK. You can add a custom getter or setter method by clicking and accessing the Getter/Setter Templates dialog.
🌐
W3Schools
w3schools.com › java › java_encapsulation.asp
Java Encapsulation and Getters and Setters
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.