SpringMVC使用@RequestParam接收参数的问题

1.背景是在写登陆接口的时候
代码如下:

@RestController
@Slf4j
public class LoginController {

    @RequestMapping("/login")
    public String login(@RequestParam(name = "userName") String userName,
                        @RequestParam(name = "password") String password) {
        log.info("userName:{},password:{}", userName, password);
        return "success->" + userName + ":" + password;
    }
}

在这里我之前因为一直用的是一个User对象来接收用户名与密码的,这次使用上面的方式来接收,使用之前传递参数的方式就会出错,于是我针对上面的接口测试了以下四种传递参数方式。

使用postman来验证:
1.Content-Type : multipart/form-data时,发送登陆请求时
在这里插入图片描述

2.Content-Type : application/x-www-form-urlencoded时,发送登陆请求
在这里插入图片描述

3.Content-Type : application/x-www-form-urlencoded,但是使用raw方式里面设置了
Text发送请求,此时的参数需要用&符号链接,也可以登陆成功

在这里插入图片描述

4.Content-Type : application/json,发送登陆请求时候报错,参数绑定错误
在这里插入图片描述

列举以上四种情况,主要是为了测试,在不是用对象接收userName和password参数时,在postman中使用userName=admin&password=admin方式传递参数,后台也是可以登陆成功。

猜你喜欢

转载自blog.csdn.net/qq_27466827/article/details/82803966