Search code examples
javaspring-bootlombokmaven-dependency

How to use Lambok dependency in SpringBoot?


I have just added Lombok dependency to my SpringBoot project so I don't have to repeat lines of getters, setters and constructors...

Here is my code example:

Book.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "Books")
public class Book {

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

    private String title;
    private String ISBN;
    private String author;
    private String issuer;
    private Integer dateOfIssue;
    private Boolean IsRented;

}

But now in my BookService.java I have all the getters and setters turned red with error saying

Cannot resolve method 'setTitle' in 'Book'

This is how I am trying to use getters and setters:

public Book updateBook(Book book){
        Book existingBook = bookRepo.findById(book.getId()).orElse(null);

        existingBook.setTitle(book.getTitle());
        existingBook.setISBN(book.getISBN());
        existingBook.setAuthor(book.getAuthor());
        existingBook.setIssuer(book.getIssuer());
        existingBook.setDateOfIssue(book.getDateOfIssue());
        existingBook.setDateOfIssue(book.getDateOfIssue());
        existingBook.setRented(book.getRented());

        return bookRepo.save(existingBook);
    }

Why is that happening? When I had my getters and setters written like:

 public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

everything was okay but as I removed that and added Lombok it seems like I can't reach to my getters and setters.


Solution

  • It seems you are using an IDE. For your IDE to recognize the code autogenerated by Lombok you need to install it on your IDE as well as having it as a dependency of your project. The web site for Lombok has instructions on how to do this. For example, if you are using Eclipse, the instructions are here.