gpt4 book ai didi

java - 无法解析客户的存储库元数据

转载 作者:行者123 更新时间:2023-12-02 02:26:04 25 4
gpt4 key购买 nike

我正在尝试将 mongodDB 与 Spring Boot 后端连接。我在本地 mongo 数据库中以 customer 的名称创建了一个小集合。我已经实现了模型、存储库类和 REST Controller ,其中包含客户的 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

最佳答案

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

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

然后输入所有 mongo 存储库的包作为基础包:

@EnableMongoRepositories(basePackages = "repo.packages")

关于java - 无法解析客户的存储库元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com