春季入门指南MongoDB REST示例



我看了Spring(https://spring.io/guides/gs/accessing-mongodb-data-rest/)的MongoDB REST入门指南。当我在不同的包中添加实体时,Application.java ,比如

com.project.rest.core.entities.Account.java

并对存储库执行相同的操作

com.project.rest.core.repositories.AccountRepo.java

构建后,应用程序无法识别 localhost:8080 下的 REST 端点。它只是显示

{
    "_links": {
        "people": {
            "href": "http://localhost:8080/people{?page,size,sort}",
            "templated": true
        },
        "profile": {
            "href": "http://localhost:8080/profile"
        }
    }
}

当我将Account.javaAccountRepo.java放在Application.java所在的同一包中时,它可以工作。

那么,如何在应用程序中集成来自不同包的存储库呢?

此致敬意

提姆

编辑:我的主要应用程序类如下所示:

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

即使添加了@ComponentScan(basePackages="com.project.rest"),Spring 引导也无法找到 AccountRepository。

存储库具有以下注释:

@RepositoryRestResource(collectionResourceRel = "accounts", path="accounts")
您需要将

Component s和Repositories放在任何子包下作为Application.java

来自春季文档

我们通常建议您将主应用程序类放在根包中,而不是其他类。@EnableAutoConfiguration注通常放在主类上,它隐式定义某些项目的基本"搜索包"。

Spring Boot 应该使用自动配置注释SpringBootApplicationEnableAutoConfiguration来处理它。

还可以通过向主应用程序类添加其他注释来指定注释ComponentScan扫描。

@ComponentScan(basePackages="com.project")

最新更新