前端的请求协议对应java的接收

  1. 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();
        }
    }
  2. 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();
        }
    }
  3. 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;
        }
    }
  4. 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;
        }
    }
  5. 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();
        }
    }
  6. 核心总结

    1. 前端 Content-Type 需与后端接收方式匹配,否则会导致解析失败。
    2. JSON 格式用 @RequestBody + 实体类接收,最适合前后端结构化数据交互。
    3. 文件上传必须用 multipart/form-data,后端通过 MultipartFile 处理。
    4. 根据实际场景选择合适的 Content-Type,并确保前后端参数名一致,即可实现高效的数据交互。
    5. 表单提交默认用 application/x-www-form-urlencoded,后端可直接用实体类接收。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值