最近刚刚接触到restful风格,即将查增删改分别用get、put、delete、post方式发送 Java后端,对前端不太了解,比较好奇前端如何向后端发送上述四种请求?
json 序列化, long->string. 怎么让restcontroller (web 前端调用用的), 转string 内部服务之间 feign接口 不转 。 现在有的feign 接口 直接复用的restcontroller
更新时我们需要发送所有的字段,而不仅仅是要更新的字段吗? 在做"fullstackopen" (https://link.segmentfault.com/?enc=07ZLLXB4MvCk2mC93psVTQ%3D%3D.KPbg%2BbSb%2F%2BS2ZtvwOphCYa%2B9v1MLz%2FiGZEin0vV7dNDpCYOuGoC4OxpTkLG6Lc9BbtAYaujNerqr1SWhmM1TFjGIATTJL0WmnNFUFnJwqoQOGaShqw0laMoqm%2FBmNRAe)的练习的时候,对这个题目描述不解?当我们只需更新"likes"字段,需要将其他的字段信息也放到请求中吗? "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241006/b759c5392b1df5385e3b893ced18e080.png) 对这个项目做一个简短的介绍 使用到的一些技术: "mongodb"、"react"、"axios"(用于前端和后端通信)、"express"、"restful " 后端相关的操作如下所示 blogRouter.put("/:id", async (request, response) => { const body = request.body; const blog = { title: body.title, author: body.author, url: body.url, likes: body.likes, }; const updateBlog = await Blog.findByIdAndUpdate(request.params.id, blog, { new: true, runValidators: true, }).populate("user", { username: 1, name: 1 }); if (updateBlog) { response.send(updateBlog); } else { response.status(404).end(); } }); * 经过测试发现,"findByIdAndUpdate"应该只会更新我们通过参数传送的字段,而其他的会保持不变。 * 我使用"postman"测试,只需要发送一个"likes"字段的信息就可以达到预期的效果,只更改了"likes"字段,其他保持不变。 那为什么题目还说要发送所有的字段呢? "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241006/2cf81ab345c46f9f1f0712585707ad9d.png) 博客的schema定义 const mongoose = require("mongoose"); const blogSchema = new mongoose.Schema({ title: { type: String, required: true }, author: String, url: { type: String, required: true }, likes: { type: Number, default: 0 }, user: { type: mongoose.Schema.Types.ObjectId, ref: "User", }, }); // https://mongoosejs.com/docs/api/document.html#Document.prototype.toJSON() blogSchema.set("toJSON", { transform: (doc, ret) => { // transform the unqiue identifier into a string representing. ret.id = ret._id.toString(); delete ret._id; // we don't require information about the mongodb version. delete ret.__v; }, }); module.exports = mongoose.model("Blog", blogSchema);