【问题标题】:Spring Boot Application IssuesSpring Boot 应用程序问题
【发布时间】:2021-01-07 01:14:08
【问题描述】:

我正在开发一个 spring boot/postgres 应用程序。这是项目结构。

src/main/java

com.test.app
com.test.app.controller
com.test.app.model
com.test.app.repository

src/test/java

com.test.app

Eclipse 抛出一个编译错误,提示“没有符合条件的 bean of type ....repository”。我必须在主类中包含以下注释。这里有什么问题?

@ComponentScan(basePackages = {"com.test.app.repository.AppRepository"})

另外,映射对我不起作用。我在控制器类中有以下注释。

@RestController
@RequestMapping("/api")

它也是 Autowired 存储库类。 http://127.0.0.1:8080/api 显示白标错误页面。有什么指点吗?

@RestController
@RequestMapping("/api")
public class AppController {
    
    @Autowired
    AppRepository appRepository;

    
    @PostMapping("/order")
    public ResponseEntity<AppEntity> createOrder(@RequestBody AppEntity order) {

    @GetMapping("/orders")
    public ResponseEntity<List<AppEntity>> getAllOrder(@RequestParam(required = false) String name) {

    @GetMapping("/order/{id}")
    public ResponseEntity<AppEntity> getOrderById(@PathVariable("id") long id) {

存储库类

@Repository
public interface AppRepository extends JpaRepository<AppEntity, Long>{
    
    List<AppEntity> findByName(String name);

}

【问题讨论】:

  • 无法准确理解您的问题是什么以及在哪里,因为没有代码,也没有任何好的提示。请阅读how to ask 一个问题,并提供minima reproducible 示例。
  • com.test.app 包下有你的主类(带有@SpringBootApplicatoin)吗?
  • com.test.app.repository.AppRepository 不是一个包,它是一个inside包的类。 (此外,如果您必须指定存储库库,您可能正在寻找 @EnableJpaRepositories,因为存储库接口不是 bean,而是在运行时生成 bean 的指令。)
  • 是的,我确实在 com.test.app 下有主类。
  • 我怀疑这是编译错误,而是弹簧检查的错误。除此之外,它不只是日食的误报吗?您是否只是尝试过运行应用程序(无论错误如何)。

标签: java spring spring-boot


【解决方案1】:

检查应用程序属性文件中是否设置了任何上下文路径“server.servlet.contextPath=/”。

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2020-10-19
    • 2020-01-09
    • 2020-08-28
    • 2020-07-13
    • 1970-01-01
    • 2021-01-14
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多