【发布时间】:2020-09-22 18:54:01
【问题描述】:
我创建了两个不同的项目,一个用于 API,一个用于 SQL,并将 SQLConnect 项目添加到 REST 项目中。
Rest 项目中的这个类
@RestController
public class PersonController {
@Autowired
private PersonRepository personRepository;
@GetMapping("/person")
public List<Person> getAllEmployees() {
return personRepository.findAll();
}
@PostMapping("/addPerson")
public Person createEmployee(@RequestBody Person person) {
return personRepository.save(person);
}
}
另一个项目项目中的接口名称是SQLConnect
@Repository
public interface PersonRepository extends JpaRepository<Person, Long>{
}
休息项目的 POM 文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
local dependencies -->
<dependency>
<groupId>com.seedq.restinterface</groupId>
<artifactId>RestInterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.seedq.sql</groupId>
<artifactId>SQLConnect</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- local dependencies -->
SQLConnect 项目的 POM 文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
错误是:
应用程序启动失败
说明:
com.seedq.rest.controller.PersonController 中的字段 personRepository 需要 'com.seedq.rest.repo.PersonRepository' 类型的 bean,但无法找到。
注入点有以下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在您的配置中定义一个 'com.seedq.rest.repo.PersonRepository' 类型的 bean。
【问题讨论】:
-
你是否在你的 RestController 中导入了 PersonRepository 接口?它在正确的路径上吗?似乎您在尝试导入它时遇到了问题。
-
是的,路径正确
标签: java spring spring-boot hibernate jpa