1 準備:
// 創建實體類
public class Person {
String name ;
Integer age;
}
public class Controller{
@requestMapper("/test")
public void test(String name ,Person person){
Sysout.out.println(name);
Sysout.out.println(person);
}
}
2 接收name 屬性
get : http://localhost:8080/get?name=123 接收值 name 爲 123
post: http://localhost:8080/get 或者使用 http://localhost:8080/get 然後將name屬性設置入body 屬性,有值。
3 接收person :
1 get : 使用 url http://localhost:8080/get?name=123&age=12
結果:person 裏的屬性被設置 ,且與person平級的name 也有值
2 post : http://localhost:8080/get 然後將name和age屬性設置入body 屬性,有值
3 使用 @reqeustBody註解,在person前家註解@reqeustBody,改變請求Content-Type 爲application/json,將body的值設置爲
{
"name":"123",
"age":12
}
可以看到結果是有值得,然後使用上述倆種方法再次進行測試,是沒有值得。
結果:@reqeustBody 只適用於post請求,且請求Content-type爲application/json ,纔可以進行數據的接收。
若果不加的話,無論是post請求中的body數據,還是通過url拼接都會將數據設置進對象內。
4 @PathVariable
獲取路徑參數。即url/{id}這種形式。
5 @RequestParam
獲取查詢參數。即url?name=這種形式
key 值必須與@requestParam 註解的name值相同。