lombok UnsatisfiedDependencyException 异常

lombok UnsatisfiedDependencyException

项目:spring-boot,maven,intellij-idea 使用 Lombok。 构建项目时,产生错误UnsatisfiedDependencyException。 不使用 Lombok 一切正常。

可在此处找到源代码https://github.com/vol2hv/moikaplus。 log.txt - 完整的启动日志

此消息已发出

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

为此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;
    }

}

定义城市资源库。

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);
}

实体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>

请告诉我在哪里查找错误?

我刚刚运行你的代码得到了following stack trace,关键部分是Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.City

Spring 引导无法持续 City class.

解决方法是添加 @Entity 注释:

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;
}

之后,代码编译成功并嵌入 tomcat 服务器,在 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)

成功编译时没有从项目中剥离 lombok。无论如何,问题仍然存在。所以我不认为龙目岛是根本原因。