【问题标题】:context initialization Issue in Spring BootSpring Boot中的上下文初始化问题
【发布时间】:2017-11-02 12:30:21
【问题描述】:

我是 Spring Boot 的新手,我正在尝试使用 Spring Boot 创建一个简单的应用程序,但出现以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:Error 创建名为'carsApplication'的bean:不满足的依赖关系 通过字段'carMongoRepository'表示;

嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建错误 名为'carMongoRepository'的bean:调用init方法 失败;嵌套异常是 org.springframework.data.mapping.model.MappingException:不能 查找域类 java.lang.Object 的映射元数据!

This is the project structure

这是我实现的 Model 类:

具有 id、make 和 model 的汽车模型类,它包括 get 和 set 方法

package com.example.cars;

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;


@Document(collection = "cars")
public class Car {

    private String id;
    private String make;
    private String model;

    public Car() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }


}

这是我实现的 Main 类:

spring boot 应用运行应用的主类

package com.example.cars;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class CarsApplication {

    @Autowired
    CarMongoRepository carMongoRepository;

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

这是我实现的 Controller 类:

用于 API 调用的控制器类,用于添加汽车值,例如 model 和 make。包括 API 调用,如下所示,具有 POST 方法实现。

package com.example.cars;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class CarController {

    @Autowired
    CarMongoRepository carMongoRepository;

    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    public void addCar(@ModelAttribute Car car) {
        carMongoRepository.save(car);
    }

}

这是我实现的存储库

为应用程序的 CRUD 存储库调用实现的接口为 如下所示。为此实现使用了@Repository 注释。 类名是 CarMongoRepository,它扩展了 CRUDRepository,如下所示。

package com.example.cars;

import org.springframework.data.repository.CrudRepository;

import com.example.cars.Car;
import org.springframework.stereotype.Repository;

@Repository
public interface CarMongoRepository extends CrudRepository {
}

当我尝试运行 spring boot 主类时,出现上述错误。所以,请帮我解决这个问题。

【问题讨论】:

    标签: java spring spring-boot spring-data-mongodb


    【解决方案1】:

    CrudRepository 需要通用参数来知道它应该处理哪个实体(或文档)。

    让您的CarMongoRepository 扩展CrudRepository<Car, String>,它应该可以工作。

    第一个参数指定存储库应处理的实体/文档的类型,第二个参数是此实体/文档的 id 的类型(我假设它是字段 idof type Stringinside车类)

    如果您不指定任何参数,它假定您的CarMongoRepository 扩展CrudRepository<Object, Object>,这就是错误消息告诉您的内容。

    org.springframework.data.mapping.model.MappingException: 无法查找域类 java.lang.Object 的映射元数据!

    您的应用程序尝试查找类型为Object 的实体/文档的映射,该类型未定义。

    我不太熟悉使用 spring 记录存储数据库,但我认为您必须在 Carclass 中使用 @Id 注释您的 id 属性(至少那是 @Entity 类的注释。也许有@Document 类的不同等价物。)

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2017-12-30
      • 2022-01-02
      • 2014-06-25
      • 2019-01-10
      • 2017-03-17
      • 2014-09-13
      • 2018-07-08
      • 1970-01-01
      相关资源
      最近更新 更多