- application/json
前端发送 JSON 数据,后端用 @RequestBody 接收并自动映射为 Java 对象。
前端示例(Axios):axios.post("/api/user", { name: "张三", age: 20 }, { headers: { "Content-Type": "application/json" } });后端 Controller:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { // 定义实体类接收 JSON 数据 static class User { private String name; private Integer age; // getter + setter } @PostMapping("/api/user") public String createUser(@RequestBody User user) { return "接收数据:" + user.getName() + ", " + user.getAge(); } } - application/x-www-form-urlencoded
前端发送表单编码数据,后端用@RequestParam或实体类接收。
前端示例(Axios):axios.post("/api/login", "username=admin&password=123", { headers: { "Content-Type": "application/x-www-form-urlencoded" } });后端 Controller:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class LoginController { // 方式1:用 @RequestParam 逐个接收 @PostMapping("/api/login") public String login( @RequestParam String username, @RequestParam String password ) { return "登录用户:" + username; } // 方式2:用实体类接收(属性名与表单 key 一致) static class LoginForm { private String username; private String password; // getter + setter } @PostMapping("/api/login2") public String login2(LoginForm form) { // 无需注解,自动映射 return "登录用户:" + form.getUsername(); } } - multipart/form-data
用于文件上传,同时可传递其他参数,后端用 MultipartFile 接收文件。
前端示例(Axios):const formData = new FormData(); formData.append("file", file); // 文件对象 formData.append("userId", 123); // 其他参数 axios.post("/api/upload", formData); // 无需手动设置 Content-Type后端 Controller:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class UploadController { @PostMapping("/api/upload") public String upload( @RequestParam("file") MultipartFile file, // 接收文件 @RequestParam("userId") Integer userId // 接收其他参数 ) { String fileName = file.getOriginalFilename(); return "上传成功:" + fileName + ",用户ID:" + userId; } } - text/plain
前端发送纯文本,后端用@RequestBody接收为字符串。
前端示例(Axios):axios.post("/api/message", "Hello World", { headers: { "Content-Type": "text/plain" } });后端 Controller:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class MessageController { @PostMapping("/api/message") public String receiveMessage(@RequestBody String message) { return "收到消息:" + message; } } - application/xml
需要引入 XML 解析依赖,后端用 @RequestBody 接收并映射为实体类。
前端示例(Axios):const xmlData = '<user><name>张三</name><age>20</age></user>'; axios.post("/api/user-xml", xmlData, { headers: { "Content-Type": "application/xml" } });后端 Controller:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class XmlController { @PostMapping("/api/user-xml") public String createUserXml(@RequestBody UserXml user) { return "XML数据:" + user.getName() + ", " + user.getAge(); } } -
核心总结
- 前端
Content-Type需与后端接收方式匹配,否则会导致解析失败。 - JSON 格式用
@RequestBody+ 实体类接收,最适合前后端结构化数据交互。 - 文件上传必须用
multipart/form-data,后端通过MultipartFile处理。 - 根据实际场景选择合适的
Content-Type,并确保前后端参数名一致,即可实现高效的数据交互。 - 表单提交默认用
application/x-www-form-urlencoded,后端可直接用实体类接收。
- 前端
前端的请求协议对应java的接收
于 2025-08-27 00:11:07 首次发布
2264

被折叠的 条评论
为什么被折叠?



