Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lombok with gradle and spring-boot

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?

like image 383
Lucas Avatar asked Nov 07 '16 22:11

Lucas


People also ask

Can gradle be used with spring boot?

Introduction. The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies . Spring Boot's Gradle plugin requires Gradle 6.8, 6.9, or 7.

Does spring boot include Lombok?

Lombok Dependency Note: If you're using a Spring Boot POM, Project Lombok is a curated dependency. Thus, you can omit the version (which will then be inherited from the Spring Boot parent POM).

How do I use Lombok annotation in spring boot?

You also need to enable annotation processing. In IntelliJ, go to File->Settings->Build, Execution, Deployment->Compiler->Annotation Processors. Select the Enable annotation processing checkbox. If you are using it with a spring boot project, you need to add this maven dependency into the project's pom file as well.


1 Answers

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.

like image 102
naXa Avatar answered Oct 14 '22 02:10

naXa