我正在尝试将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));
}
这是堆栈跟踪:
答案 0 :(得分:0)
我认为添加mongo存储库必须与注释@EnableMongoRepositories一起添加到spring上下文中:
然后输入所有mongo仓库的软件包作为基本软件包:
@EnableMongoRepositories(basePackages = "repo.packages")