Using lombok with gradle and spring-boot

Lucas picture Lucas · Nov 7, 2016 · Viewed 48.7k times · Source

I am trying to build a project with lombok and this is what I have as dependencie.

dependencies {
   compile("org.springframework.boot:spring-boot-starter-thymeleaf")
   compile("org.springframework.social:spring-social-facebook")
   compile("org.springframework.social:spring-social-twitter")
   testCompile("org.springframework.boot:spring-boot-starter-test")
   testCompile("junit:junit")
   compile("org.springframework.boot:spring-boot-devtools")
   compile("org.springframework.boot:spring-boot-starter-data-jpa")
   compile("mysql:mysql-connector-java")
   compileOnly("org.projectlombok:lombok:1.16.10")
}

I am able to include the anotations, and I have included lombok in the editor. I am even able to compile a code using lombok and making a cal to a method generated by lombok.

This is my entity:

@Data
@Entity
@Table(name = "TEA_USER", uniqueConstraints = {
    @UniqueConstraint(columnNames = { "USR_EMAIL" }),
    @UniqueConstraint(columnNames = { "USR_NAME" })
})
public class User {


   @NotNull
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="USR_ID")
   private long id;

   @NotNull
   @Column(name="USR_FNAME")
   private String firstName;

   @NotNull
   @Column(name="USR_LNAME")
   private String lastName;


   @NotNull
   @Min(5)
   @Max(30)
   @Column(name="USR_NAME")
   private String username;

   @Column(name="USR_EMAIL")
   private String email;

   @Min(8)
   @NotNull
   @Column(name="USR_PASSWORD")
   private String password;
}

And this is a function that compiles fine:

@PostMapping("/registration/register")
public String doRegister (@ModelAttribute @Valid User user, BindingResult result){
    user.getEmail();
    System.out.println(user.getFirstName());
    if (result.hasErrors()) {
         return "register/customRegister";
    }
    this.userRepository.save(user);
    return "register/customRegistered";
}

But when I run bootRun and I try to access the funcionality this is the Exception I get:

org.springframework.beans.NotReadablePropertyException: Invalid property 'firstName' of bean class [com.lucasfrossard.entities.User]: Bean property 'firstName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

But, if I manually include the setter and getters, this works fine. I don't get whats going on and how to fix it. Any idea?

Answer

naXa picture naXa · Nov 8, 2018

With the latest Lombok 1.18 it's simple. io.franzbecker.gradle-lombok plugin is not required. Example dependencies from my project:

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
    implementation "org.springframework.social:spring-social-facebook"
    implementation "org.springframework.social:spring-social-twitter"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"

    testImplementation "org.springframework.boot:spring-boot-starter-test"

    runtimeClasspath "org.springframework.boot:spring-boot-devtools"
    runtime "mysql:mysql-connector-java"

    // https://projectlombok.org
    compileOnly 'org.projectlombok:lombok:1.18.4'
    annotationProcessor 'org.projectlombok:lombok:1.18.4'
}

Other suggestions:

  1. testCompile("junit:junit") is not required because the spring-boot-starter-test “Starter” contains JUnit.
  2. Use implementation or api configuration instead of compile (since Gradle 3.4). How do I choose the right one?
  3. Do not use compile configuration for devtools. A tip from Developer Tools guide:

    Flagging the dependency as optional in Maven or using a custom developmentOnly configuration in Gradle (as shown above) is a best practice that prevents devtools from being transitively applied to other modules that use your project.

  4. If you use IntelliJ IDEA, make sure you have Lombok plugin installed and Annotation Processing enabled. To make an initial project setup easier for newcomers you can also specify the Lombok plugin as required.