• SpringBoot 接收不到 post 请求数据与接收 post 请求数据


    文章归档:https://www.yuque.com/u27599042/coding_star/xwrknb7qyhqgdt10

    SpringBoot 接收不到 post 请求数据

    1. 接收 post 请求数据,控制器方法参数需要使用 RequestParam 注解修饰
    public BaseResponseResult<Object> getMailCode(
        @RequestParam("mail") String mail
    ) {}
    
    • 1
    • 2
    • 3
    1. 前端发送 post 请求时,请求数据类型(Content-Type)应该为 application/x-www-form-urlencoded

    默认情况下,使用 axios.post() 发送 post 请求,请求的数据类型为 application/json

    使用 axios 发送 post 请求,且请求数据类型为 application/x-www-form-urlencoded

    /**
     * 发送 post 请求,请求数据类型(Content-Type)为 application/x-www-form-urlencoded
     * 
     * @param url 请求资源路径
     * @param data 请求数据
     * @return {Promise>}
     */
    export function postContentTypeFormUrlencoded(url, data) {
        return request.post(
            url,
            data,
            {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }
        )
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. 如果前后端 post 数据交互,需要使用 json 格式的数据,那么前端使用 axios.post() 发送 post 请求,后端接收数据时最好使用自定义类对象形式的参数,且参数使用 RequestBody 注解修饰
    public BaseResponseResult<Object> loginByMail(
                @RequestBody
                UserLoginInfoVo userLoginInfoVo
        ) {}
    
    • 1
    • 2
    • 3
    • 4
    /**
     * 用于封装接收客户端传递到服务端的用户登录信息
     */
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Getter
    @Setter
    public class UserLoginInfoVo {
    
        /**
         * 用户通过密码登录时使用的用户名/账号/电子邮箱/手机号
         */
        private String account;
    
        /**
         * 用户通过邮箱登录时使用的电子邮箱。
         * 需要使用 RSA 加密算法进行解密
         */
        private String mail;
    
        /**
         * 用户通过手机登录时使用的手机号。
         * 需要使用 RSA 加密算法进行解密
         */
        private String telephone;
    
        /**
         * 用户登录时输入的验证码。
         * 需要使用 RSA 加密算法进行解密
         */
        private String code;
    
        /**
         * 用户通过密码登录输入的密码。
         * 需要使用 RSA 加密算法进行解密
         */
        private String password;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    考研数学一二三 2010-2019年每道题的难度系数
    c语言(函数栈帧的创建和销毁)
    H5 手机键盘兼容
    Elasticsearch上手指南
    基于Matlab实现图像配准技术(附上源码+图像)
    正则表达式 包含一些 但不包括 的命令
    Junit单元测试
    如何使用DBeaver连接Hive
    “可编程网络”的基础概念介绍
    阿里云部署开源MQTT平台mosquitto的docker操作
  • 原文地址:https://blog.csdn.net/m0_53022813/article/details/134061895