最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Lombok generated getters and setters are accessible but don't work - Stack Overflow

programmeradmin1浏览0评论

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 .springframework.web.bind.annotation.PostMapping;
import .springframework.web.bind.annotation.RequestBody;
import .springframework.web.bind.annotation.RequestMapping;
import .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=".0.0" xmlns:xsi=";
    xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>.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>.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>.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.

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 .springframework.web.bind.annotation.PostMapping;
import .springframework.web.bind.annotation.RequestBody;
import .springframework.web.bind.annotation.RequestMapping;
import .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./POM/4.0.0" xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache./POM/4.0.0 https://maven.apache./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>.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>.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>.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.

Share Improve this question asked Feb 4 at 14:09 coldkeycoldkey 91 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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)

  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.

Following @shshi009, once you are on this setting panel (Build,Execution,Deployment > Annotation Processors) select your application module and in the right side activate "Obtain processors from project classpath" - which is deactivated by default.

发布评论

评论列表(0)

  1. 暂无评论