表单格式:form-data、x-www-form-urlencoded
非表单格式:json、xml、html、text等等
post(表单格式数据)和get请求都可以用,不可以接收post除表单之外的格式数据
public AjaxResult sendSms(HttpServletRequest req) throws Exception {
System.out.println(req.getParameter("phoneValue"));
return AjaxResult.success();
}
适用于get、post(表单格式数据)都可以。不可以接收post除表单之外的格式数据
直接在方法中写上参数的名字,参数名字和请求中参数名字必须一样
public AjaxResult sendSms(String phoneValue) throws Exception {
System.out.println(phoneValue);
return AjaxResult.success();
}
springboot的@RequestParam不好使,需要设置它required = false,defaultValue = "1".推荐上面的方法
@GetMapping("/sendsms")
public AjaxResult sendSms(@RequestParam(value = "s",required = false,defaultValue = "1") String s,@RequestParam(value = "n") String n) throws Exception {
System.out.println(s1+n);
return AjaxResult.success();
}
post(表单格式)和get都可以来接收
- 用bean接收get请求的参数
@GetMapping("/sendsms")
public AjaxResult sendSms(User user) throws Exception {
System.out.println(user.toString());
return AjaxResult.success();
}
//打印
User{id=null, name='132213', age=123, identity='null', height=187.0, weight=null, sex='null', attribute='null', starttime=null, currentprovince='null', currentcity='null', currentcounty='null', purpose='null', account='null', picture='null'}
- 用bean接收post请求参数
@PostMapping("/sendsms")
public AjaxResult sendSms(User user) throws Exception {
System.out.println(user.toString());
return AjaxResult.success();
}
//打印
//User{id=null, name='132213', age=123, identity='null', height=187.0, weight=null, sex='null', attribute='null', starttime=null, currentprovince='null', currentcity='null', currentcounty='null', purpose='null', account='null', picture='null'}
适合post请求,如果没有RequestBody会报错。只关系请求体中有没有数据数据,没有就会报错。
处理除表单之外的其他格式的post数据,例如json、xml等等
用@RequestBody接收Json格式的数据,并用bean来接收
@PostMapping("/sendsms")
public String sendSms(@RequestBody User user) throws Exception {
System.out.println(user);
return user.toString();
}
//打印效果
//User{id=null, name='2', age=1, identity='null', height=null, weight=null, sex='null', attribute='null', starttime=null, currentprovince='null', currentcity='null', currentcounty='null', purpose='null', account='null', picture='null'}