【问题标题】:Cannot Resolve Repository Metadata for Customers无法为客户解析存储库元数据
【发布时间】:2019-07-29 12:08:21
【问题描述】:

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

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

下面是我写的代码:

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));
      }

这是堆栈跟踪:

【问题讨论】:

  • 请在问题中添加堆栈跟踪。
  • 发布你得到的错误跟踪!
  • 图片已上传

标签: java mongodb maven spring-boot


【解决方案1】:

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

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

然后你输入所有 mongo repos 的包作为基础包:

@EnableMongoRepositories(basePackages = "repo.packages")

【讨论】:

  • 我遵循的教程没有提到这一点。让我试试
  • 即使添加此注释后,响应仍然保持不变(“消息”:“无法为客户解析存储库元数据。”)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多