简体   繁体   中英

Lombok not recognizing getters and setters

I am trying to set up lombok in my sts.

So far I have done these steps: I downloaded the lambok jar file. I ran the file and specified the path for sts.exe and then clicked on install. I have added the required dependencies in my pom.xml

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

I have also edited my sts.ini file. After installation of lambok.jar following line was already there at the end pf the file

-javaagent:E:\JAVA SOFTWARES\spring-tool-suite-3.9.8.RELEASE-e4.11.0-win32-x86_64\sts-bundle\sts- 
 3.9.8.RELEASE\lombok.jar

so I moved it next to

-vmargs

Then, I cleaned my project. I have also updated my project. Closed sts and then ran my application again. But it is still not recognizing the getters in my file. It produces the following error.

  The method getFirstname() is undefined for the type Student

Student.java:

package com.crud.msstudent.models;

 import java.io.Serializable;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain=true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@NotEmpty(message = "First name is required")
private String firstname;

@NotEmpty(message = "Last name is required")
private String lastname;

@Column(nullable = true, name = "email")
private String email;

}

The error is being shown in my StudentController.java file. Following is some of the code:

 @PutMapping(value="/students/{id}")
public Student updateStudent(@PathVariable("id") @Min(1) int id, @Valid 
@RequestBody Student newstd) {
    Student stdu = studentservice.findById(id)
                                 .orElseThrow(()->new 
  StudentNotFoundException("Student with "+id+" is Not Found!"));
    stdu.setFirstname(newstd.getFirstname());
    stdu.setLastname(newstd.getLastname());
    stdu.setEmail(newstd.getEmail());
    return studentservice.save(stdu); 
}           
@DeleteMapping(value="/students/{id}")
public String deleteStudent(@PathVariable("id") @Min(1) int id) {
    Student std = studentservice.findById(id)
                                 .orElseThrow(()->new 
StudentNotFoundException("Student with "+id+" is Not Found!"));
    studentservice.deleteById(std.getId());
    return "Student with ID :"+id+" is deleted";            
}

Please tell me what am I missing?

If you're using STS you might be missing the plugin required for it. For further details please refer to the Lombok site on how to install it.

If mvnw.cmd clean compile command works normally, try project reloading.

Right click your project folder in Package Explorer > Maven > Update Project

在此处输入图像描述

are you using Eclipse IDE? in some case, you have to update the project explicitly in eclipse.

better to go without using lombok

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@NotEmpty(message = "First name is required")
private String firstname;

@NotEmpty(message = "Last name is required")
private String lastname;

@Column(nullable = true, name = "email")
private String email;

public Student()
 {
  }

 public Student(int id,String firstname,String lastname,String email)
 {
          this.id=id;
          this.firstname=firstname;
          this.lastname=lastname;
          this.email=email
   
 }



public int getId()
    {
    return id;
    }
   public void setId(int id)
   {
       this.id=id;
   }

   public String getFirstname()
    {
    return firstname;
    }
   public void setFirstname(String firstname)
   {
       this.firstname=firstname;
   }

   public String getLastname()
    {
    return lastname;
    }
   public void setLastname(String lastname)
   {
       this.lastname=lastname;
   }

public String getEmail()
    {
    return email;
    }
   public void setEmail(String email)
   {
       this.email=email;
   }
 
  
 

Does this help (source) :

To include lombok as a 'provided' dependency, add it to your block like so:

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

JDK9+ with module-info.java The configuration of the compiler plug-in should contain the following:

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </path>
</annotationProcessorPaths>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM