【发布时间】:2021-11-05 03:13:39
【问题描述】:
当尝试使用 PostMapping 和 PUT 请求更新 mongo db 表中的现有条目时,每次我尝试更新表中都会创建一个新条目..
以下是我的代码 控制器
@RestController
@RequestMapping("/expenses")
public class mydataExpController {
@PutMapping("/")
public mydataExpense updExpenses(@RequestBody mydataExpense exp){
return expenseServices.updData(exp);
}
服务
@Service
public class ExpenseServices {
@Autowired
mydataExpRepository mydataexprepository;
public mydataExpense updData(mydataExpense exp) {
return mydataexprepository.save(exp);
}
存储库
@Repository
public interface mydataExpRepository extends MongoRepository<mydataExpense, String> {
}
在邮递员中
connection →keep-alive
content-type →application/json
date →Tue, 29 Dec 2020 10:54:51 GMT
keep-alive →timeout=60
transfer-encoding →chunked
每次我使用 PUT 执行 http://localhost:8080/expenses/
在数据库中创建一个新条目
【问题讨论】:
-
你能添加一个示例内容吗?很可能您没有正确传递 id 字段。
-
请看传递的内容和响应
-
{ "_id": "5fedba90e11a563ce37e9890", "expdate": "12-24-2020", "exptyp": "Biriyani Hub", "expamt": 0 }
-
{ "expdate": "12-24-2020", "exptyp": "Biriyani Hub", "expamt": 0 }
标签: java mongodb spring-boot microservices rest