Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Builder Annotation not working in Java class


This code was written for learnig purpose. @Builder and @Data annotation has been implemented in the Book class. While trying to create a builder for the same in LibraryApplication.java builder is not being recognized.

How can I enable the annotations which on Book class

**Edit **: When trying to compile below error is generated.
java.lang.Error: Unresolved compilation problem: The method builder() is undefined for the type Book at com.baeldung.LibraryApplication$DataSetup.run(LibraryApplication.java:25)

Also I am using STS implementation of eclipse. I have checked whether the same code works in eclilpse Oxygen but the same error is thrown

Note: The project is created in Spring Boot

Book.class

package com.baeldung.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@Builder
@Entity
@AllArgsConstructor
public class Book {

@Id
@GeneratedValue
private Long id;

@NotNull
String name, isbn;

}

LibraryApplication.java

package com.baeldung;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import com.baeldung.domain.Book;
import com.baeldung.repo.BookingRespository;

@SpringBootApplication
public class LibraryApplication {

@Autowired
private BookingRespository bookingRespository;

@Component
class DataSetup implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments args) throws Exception {
//             bookingRespository.save(Book.builder().name("Way of Kings").isbn("123").build());

    }

}

public static void main(String[] args) {
    SpringApplication.run(LibraryApplication.class, args);
}

}

BookingRepository.java

package com.baeldung.repo;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.baeldung.domain.Book;

@RepositoryRestResource(path="books",collectionResourceRel="books")
public interface BookingRespository extends PagingAndSortingRepository<Book, Long>{

}

pom.xml

<?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 http://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>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>library</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>27.0.1-jre</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

like image 326
PAUL JOY Avatar asked Feb 06 '26 02:02

PAUL JOY


1 Answers

As per suggestion by @f1sh - If you use mvn package, this should work. But to get it to work in your IDE (which is probably where you get this error), you need to install lombok into eclipse: projectlombok.org/setup/eclipse

like image 59
PAUL JOY Avatar answered Feb 07 '26 15:02

PAUL JOY