Java 使用Spring和MongoDB的RESTAPI

Java 使用Spring和MongoDB的RESTAPI,java,spring,mongodb,rest,spring-mvc,Java,Spring,Mongodb,Rest,Spring Mvc,我一直在使用Spring开发restapi,其中MongoDB用于数据库 以下是我目前的代码: User.java: @Id private String id; private String firstName; private String lastName; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } p

我一直在使用
Spring
开发restapi,其中
MongoDB
用于数据库

以下是我目前的代码:

User.java:

@Id
private String id;
private String firstName;
private String lastName;
private String address;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
用户存储库接口:

public interface UserRepository extends MongoRepository<User, String>{

}
这就是java类的全部内容,我运行了
MongoDB
实例。当我尝试启动
tomcat
服务器时,它会给出一个异常
BeanNotFoundException

如何解决这个问题

这是我得到的一个例外:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cg.mongo.service.UserRepository com.cg.mongo.controller.UserRestController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cg.mongo.service.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4810)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

您没有使用Auto configuration@EnableAutoConfiguration,并且您的配置类中没有@enablemongorepositiories,因此您的存储库没有加载到Springbeans中,我认为您需要将@enablemongorepositiories添加到您的配置类中,并设置存储库的包名,用户还必须拥有@Document和@Id,而不是JPA。 并确保您的maven pom文件

- <dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-mongodb</artifactId> 
  </dependency>
- <dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-rest</artifactId> 
  </dependency>
-
org.springframework.boot
spring启动程序数据mongodb
- 
org.springframework.boot
弹簧启动启动器数据rest

请仔细阅读您的错误。您的答案在异常stackTrace()中


org.springframework.beans.factory.BeanCreationException:创建名为
userRestController的bean时出错:自动连线依赖项的注入失败
;嵌套异常为
org.springframework.beans.factory.BeanCreationException
:无法自动连接字段:private
com.cg.mongo.service.UserRepository
com.cg.mongo.controller.UserRestController.repo
;嵌套异常为
org.springframework.beans.factory.NoSuchBeanDefinitionException
:未找到依赖项类型为
[com.cg.mongo.service.UserRepository]
的符合条件的bean:至少应1个符合此依赖项autowire候选项的bean

未使用自动配置@EnableAutoConfiguration,并且您的配置类中没有@enablemongorepositions


谢谢

看来它找不到合适的bean来自动连线。你能写下哪个bean无法解析吗,因为我在screenschot上看不到它吗?@NikolayTomitov我添加了一个异常。它似乎是为com.cg.mongo.service.UserRepository而来的。不是吗?@SandunkaMihiran我修正了我的答案你必须使用EnableMongorPositories和maven来获得spring启动数据mongodb
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.cg.mongo")
public class MongoSeparateServerConfiguration {

}
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cg.mongo.service.UserRepository com.cg.mongo.controller.UserRestController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cg.mongo.service.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4810)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
- <dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-mongodb</artifactId> 
  </dependency>
- <dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-rest</artifactId> 
  </dependency>