javamavenintellij-idealombokintellij-lombok-plugin

Lombok generated getters and setters are accessible but don't work


Controller: To the endpoint I sent { "name" : "someSensorName" } json and its supposed to save it

package kz.don.sensorTracker.controller;

import kz.don.sensorTracker.model.Sensor;
import kz.don.sensorTracker.service.SensorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sensors")
@RequiredArgsConstructor
public class SensorController {

    private SensorService sensorService;

    @PostMapping("/registration")
    public Sensor save(@RequestBody Sensor sensor) {
        Sensor sensor1 = new Sensor();
        sensor1.setName("Test");
        return sensorService.save(sensor);
    }

}

Model: Simple Sensor entity with JPA

package kz.don.sensorTracker.model;

import jakarta.persistence.*;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;

@Setter
@Getter
@Entity
@Table(name = "SENSORS")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Sensor implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    private LocalDateTime initDate;

    @PrePersist
    public void prePersist(){
        initDate = LocalDateTime.now();
    }

}

pom.xml: I tried to specifically provide newer version of lombok to tackle the problem, yet nothing changed.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>kz.don</groupId>
    <artifactId>sensorTracker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sensorTracker</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
/Users/coldkey/Downloads/sensorTracker/src/main/java/kz/don/sensorTracker/controller/SensorController.java:23:16java: cannot find symbolsymbol:   method setName(java.lang.String)location: variable sensor1 of type kz.don.sensorTracker.model.Sensor

build failed - thats the whole stack trace

With manually IDEA generated getters and setters it works perfectly however the IDEA itself suggests to use lombok.

The error occurs every time I try to use methods generated by lombok, however the constructor works okay with all-args and no-args ones.


Solution

  • This might be an issue with lombok plugin in IntelliJ, you can try following steps -

    1. Make sure lombok plugin is installed
    2. Make sure you have "Enable Annotation Processing" option checked (Build, Execution, Deployment -> Compiler -> Annotation Processors)

    IntelliJ Annotation Processors Config

    1. Invalidate Caches and Restart

    If the error is still there, then try to build and run from the command line. Sometimes intellij build throws compilaion error due to lombok issue, but it runs fine from command line as the code itself has correct lombok dependency and configuration.

    For example, Run mvn clean install for maven build or mvn spring-boot:run to run a spring boot application from a terminal.