文章归档:https://www.yuque.com/u27599042/coding_star/xwrknb7qyhqgdt10
SpringBoot 接收不到 post 请求数据
- 接收 post 请求数据,控制器方法参数需要使用 RequestParam 注解修饰
public BaseResponseResult<Object> getMailCode(
@RequestParam("mail") String mail
) {}
- 前端发送 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<axios.AxiosResponse<any>>}
*/
export function postContentTypeFormUrlencoded(url, data) {
return request.post(
url,
data,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
}
- 如果前后端 post 数据交互,需要使用 json 格式的数据,那么前端使用 axios.post() 发送 post 请求,后端接收数据时最好使用自定义类对象形式的参数,且参数使用 RequestBody 注解修饰
public BaseResponseResult<Object> loginByMail(
@RequestBody
UserLoginInfoVo userLoginInfoVo
) {}
/**
* 用于封装接收客户端传递到服务端的用户登录信息
*/
@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;
}