Estou estudando criação de APIs REST com Spring Boot seguindo o seguinte tutorial: Building REST services with Spring.
Porem ao implementar ao classe LoadDatabase do tutorial estou recebendo recebendo um erro de compilação ao atualizar a variável log, mesmo com o método estando com a anotação @Slf4j.
NOTA: Projeto criado no Spring Iniializr, código escrito usando Eclipse, e executado com Maven por linha de comando usando ./mvnw spring-boot:run
.
Esta é a classe LoadDatabase
package com.ricardo.studySpring.loaders;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ricardo.studySpring.entities.Product;
import com.ricardo.studySpring.repotories.ProductRepository;
import lombok.extern.slf4j.Slf4j;
@Configuration
@Slf4j
public class LoadDatabase {
@Bean
public CommandLineRunner initDatabase(ProductRepository repository) {
return args -> {
log.info("Preloading " + repository.save(new Product("Banan", 2.25)));
log.info("Preloading " + repository.save(new Product("carne", 20.0)));
};
}
}
Este é o arquivo 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 https://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.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ricardo</groupId>
<artifactId>studySpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>studySpring</name>
<description>Study of Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Este é o log de saída Maven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.724 s
[INFO] Finished at: 2019-10-02T21:01:14-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.8.RELEASE:run (default-cli) on project studySpring: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'loadDatabase' defined in file [/home/ricardo/workspaces/projws-backend/studySpring/target/classes/com/ricardo/studySpring/loaders/LoadDatabase.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ricardo.studySpring.loaders.LoadDatabase$$EnhancerBySpringCGLIB$$42d56eee]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
[ERROR] log cannot be resolved
[ERROR] log cannot be resolved
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException