lombok UnsatisfiedDependencyException

时间:2017-08-19 09:36:21

标签: maven spring-boot intellij-idea lombok

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

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

发出此消息

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

这个班级

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

}

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

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

请告诉我,在哪里寻找错误?

1 个答案:

答案 0 :(得分:2)

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

Spring Boot无法持久保存City类。

解决方法是向其添加@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。无论如何问题仍然存在。所以我不认为龙目岛是根本原因。