无法为客户解析存储库元数据

时间:2019-07-29 12:08:21

标签: java mongodb maven spring-boot

我正在尝试将mongodDB与Spring Boot后端连接。我已经在本地mongo数据库中以 customer 的名称收集了一个小集合。我已经实现了模型,存储库类和REST控制器,其中包含针对客户的GET,POST和DELETE端点。

每当我启动项目并尝试达到终点时,都会出现以下错误:

java.lang.IllegalArgumentException: Could not resolve repository metadata for customers

下面是我编写的代码:

Customer.java(模型类)

package fashion.connect.models;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection= "customers")
public class Customers {

    @Id
    public ObjectId _id;
    public  String firstname;
    public String lastname;

    public Customers() {}

    public Customers(ObjectId _id, String firstname, String lastname) {
        this._id = _id;
        this.firstname= firstname;
        this.lastname= lastname;
    }

    public String get_id() {
        return _id.toHexString();
    }
    public void set_id(ObjectId _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;
    }
}

CustomerRepository.java

package fashion.connect.repositories;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import fashion.connect.models.Customers;

@Repository
public interface CustomersRepository extends MongoRepository<Customers, String> {
    Customers findBy_id(ObjectId _id);
}

CustomerController.java

package fashion.connect.controllers;

import fashion.connect.models.Customers;
import fashion.connect.repositories.CustomersRepository;

import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/customers")
public class CustomersController {
      @Autowired
      private CustomersRepository repository;

      @RequestMapping(value = "/", method = RequestMethod.GET)
      public List<Customers> getAllCusomers() {
        return repository.findAll();
      }

      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public Customers getCustomertById(@PathVariable("id") ObjectId id) {
        return repository.findBy_id(id);
      }

      @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
      public void modifyCustomerById(@PathVariable("id") ObjectId id, @Valid @RequestBody Customers customers) {
          customers.set_id(id);
          repository.save(customers);
      }

      @RequestMapping(value = "/", method = RequestMethod.POST)
      public Customers createCustomer(@Valid @RequestBody Customers customers) {
        customers.set_id(ObjectId.get());
        repository.save(customers);
        return customers;
      }

      @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
      public void deleteCustomert(@PathVariable ObjectId id) {
        repository.delete(repository.findBy_id(id));
      }

GET api response

这是堆栈跟踪:

Stack Trace

1 个答案:

答案 0 :(得分:0)

我认为添加mongo存储库必须与注释@EnableMongoRepositories一起添加到spring上下文中:

https://docs.spring.io/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.html

然后输入所有mongo仓库的软件包作为基本软件包:

@EnableMongoRepositories(basePackages = "repo.packages")