简体   繁体   中英

Generating getters and setters using project Lombok

I wanted to use Lombok dependency in my project. So, I downloaded lombok-1.16.18.jar and added to the build path of on of my classes. The configuration is shown below.

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString(includeFieldNames=true)
public @Data class Student {

@Getter
@Setter
private Integer id;

@Getter
@Setter
private String name;

//private Date dob;
@Getter
@Setter
private String uid;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student s = new Student();
    System.out.println(s);


}
}

But, I am not getting proper output in console. I am getting Object classes toString() output like com.selflearn.sandesha.Student@7852e922 . I am also not able to use getters and setters. How to make Lombok work or what wrong I am doing?

Your configuration is incomplete. Review https://projectlombok.org/setup/eclipse and check if It compiles. When it does, try again!

If you use netbeans go to properties->Build->compiling for checked the option Enable Annotation processing

or search this option in your editor

thanks for your help. In eclipse IDE, I was not exporting the lombok jar. Even though, I added jar to the build path, it seems I should export it to the project in order and export section of eclipse.

So, In eclipse editor,

right click on your project --> Build Path --> Configure Build Path... -->select order and export tab and check the jar you want to export to your project.

Or

you can also simply run the downloaded jar. This will detect the possible IDE's in the system and configure it to support all lombok features.

This solved my issue. Again thank you for your support.

Simply download the lombok-1.18.24.jar(any version) and once download, double click on that file, so it checks your system and detects the IDE's that are available on your system. Then verify the IDE's and click on install/update button. Once the above process is completed, restart your IDE maybe, sts or eclipse.

@Data on class level would be sufficient. Maybe it is a problem that you have put the @Data annotation between public and class .

Other question is whether your lombok.jar is on the classpath.

If I would rewrite your class, I would come up with the following:

@Data
public class Student {

private Integer id;

private String name;

private String uid;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student s = new Student();
    System.out.println(s);


}
}

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