GET请求
共同点
都是从get请求中获取参数.
异同点
PathVariable适用于restful请求方式
@PathVariable将URL中的占位符参数绑定到控制器处理方法的入参
/**
* 测试动态接收URL中的数据
* */
@RequestMapping(value = "/test2/{pageNo}/{pageSize}", method = RequestMethod.POST)
public String urlData(@PathVariable int pageNo , @PathVariable int pageSize){
String result = "receive pageNo = "+pageNo+" pageSize = "+pageSize;
System.out.println(result);
return result;
}
RequestParam请求方式
会把传递过来的Form表单中的name对应到formData方法的name参数上
/**
* 测试接收form表单、URL的数据。不能接收Json数据
* */
@RequestMapping(value = "/test1", method = RequestMethod.POST)
public String formData(@RequestParam("name") String name , @RequestParam("age") int age){
String result = "receive name = "+name+" age = "+age;
System.out.println(result);
return result;
}
POST请求
@RequestBody
@RequestBody只接受post请求, json格式数据
@RequestBody 接收Json格式的数据需要加这个注解。该注解不能接收URL、Form表单传参
/**
* 测试接收json数据
* */
@RequestMapping(value = "/jsonData", method = RequestMethod.POST)
public String jsonData(@RequestBody TestModel tm){
String result = "receive name = "+tm.getName()+" age = "+tm.getAge();
System.out.println(result);
return result;
}