- 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
,后端可直接用實體類接收。
- 前端?