Search code examples
spring-bootintellij-idealombokintellij-pluginintellij-lombok-plugin

Using lombok dependency in my spring-boot project but the getter method yields error at runtime (built successfully though)


I am developing a spring-boot project using Gradle as the build tool on ItelliJ IDE.

I have the dependency of lombok declared in gradle.build:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.5.3'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compileOnly 'org.projectlombok:lombok:1.18.20'
}

I have a model class:

import lombok.Data;
import java.math.BigDecimal;

@Data
public class ProductModel {
    private String name;
    private BigDecimal price;
    private Integer quantity;
}

As you can see I have annotated with @Data.

My controller has the method to handle a POST request, its payload is mapped to the ProductModel:

@PostMapping
public String createProduct(@RequestBody ProductModel productPayload) {
       // Runtime error: error: cannot find symbol, 'getName' in 'ProductModel'
       productPayload.getName();
}

I know I need to install the lombok plugin on my IntelliJ IDE in order to avoid compiler error on the getter method. So I did that. But when I run my application I get error:

error: cannot find symbol
symbol:   method getName()
location: variable productPayload of type CreateProductRestModel

I also tried change the dependency from compileOnly to implementation:

implementation 'org.projectlombok:lombok:1.18.20'

It doesn't help. Why is that? What am I missing?

(I have enabled annotationProcessor on my IntelliJ too)


Solution

  • In order for Gradle to pick up on annotation processors, they have introduced a separate configuration that will generate all the new code ahead of the "normal" compilation.

    For Lombok, it would look something like this:

    dependencies {
        compileOnly 'org.projectlombok:lombok:1.18.20'
        annotationProcessor 'org.projectlombok:lombok:1.18.20'
        
        testCompileOnly 'org.projectlombok:lombok:1.18.20'
        testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
    }