【发布时间】:2026-01-16 20:15:02
【问题描述】:
我正在尝试使用 MongoDB 连接到 mLab 来设置 Spring Boot 应用程序。但是当我尝试运行这个测试(或任何其他测试)时
@RunWith(SpringRunner.class)
@SpringBootTest
public class SyncrewApplicationTests {
@Test
public void contextLoads() {
}
}
我得到这个错误:
Caused by: java.lang.ClassNotFoundException: org.springframework.data.rest.core.invoke.RepositoryInvokerFactory
我的 Spring 启动应用程序:
@SpringBootApplication
public class SyncrewApplication {
public static void main(String[] args) {
SpringApplication.run(SyncrewApplication.class, args);
}
}
用户 MongoDB 存储库
@RepositoryRestResource(exported = false)
public interface UserRepository extends MongoRepository<User, Integer> {
User findUserByUsername(String username);
User findUserByEmail(String email);
}
用户类
@Document(collection = "users")
public class User implements Serializable, UserDetails {
@Id
private String id;
private String username;
public User(){}
public User(String username){
this.username = username;
}
// getters and setters
我认为我的问题出在我的 pom.xml 中,但我不知道在哪里.. 这是我的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
【问题讨论】:
-
你运行这个应用程序时是否下载并运行了mongodb?
标签: java mongodb maven spring-mvc spring-boot