Vladimir Madorin
Vladimir Madorin

Reputation: 123

lombok UnsatisfiedDependencyException

Project: spring-boot,maven,intellij-idea using Lombok. When building a project, an error UnsatisfiedDependencyException is generated. Without using Lombok everything works fine.

Source code can be found here https://github.com/vol2hv/moikaplus. log.txt - Full launch log

This message is issued

Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'repository'.

for this class

package io.khasang.moikaplus.controller;
import io.khasang.moikaplus.dao.CityRepository;
import io.khasang.moikaplus.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/test")
public class TestController {
    @Autowired
    private CityRepository repository;

    @RequestMapping
    public City res (){
        return repository.findOne(3);
    }
    @RequestMapping("/all")
    public List<City> cityList(){
        List<City> cityList = new ArrayList<>();
        repository.findAll().forEach(cityList::add);
        return cityList;
    }

}

Definition CityRepository.

package io.khasang.moikaplus.dao;

import io.khasang.moikaplus.entity.City;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource
public interface CityRepository extends CrudRepository<City, Integer> {
    List<City> findByName(@Param("name") String name);
}

Entity City.java

package io.khasang.moikaplus.entity;
import lombok.Data;
import javax.persistence.Id;
@Data
public class City{
    @Id
    private int id;
    private String name;
    private String region;
}

SpringBootWebApplication.java

package io.khasang.moikaplus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

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

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>

    <groupId>io.khasang</groupId>
    <artifactId>moikaplus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>moikaplus</name>
    <description>Мойка на Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--postgresql-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--spring-boot-starter-data-rest-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <!--spring-boot-starter-test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>1.4.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

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

</project>

Tell me, please, where to look for a mistake?

Upvotes: 1

Views: 838

Answers (1)

DevDio
DevDio

Reputation: 1564

I have just run your code and got following stack trace, the crucial part is Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.City.

Spring Boot could not persist the City class.

The solution for this was to add @Entity annotation to it:

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;

@Data
@Entity
public class City{
    @Id
    private int id;
    private String name;
    private String region;
}

After that the code compiles successfully and embedded tomcat servers starting fine in IntelliJ:

2017-08-19 12:41:05.703 INFO 10664 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.651 seconds (JVM running for 4.312)

Stripping lombok from the project did not occur in successful compilation. The problem persisted anyway. So I do not consider the Lombok as the root cause.

Upvotes: 2

Related Questions