【发布时间】: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