@RequestBody
取得request body參數
用法: 只能加在方法的參數上
用途: 取得request body裡面的參數(將 json 轉為 java object)
POST
{”id”: 36,”name”:”ts”}
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test(@RequestBody Student student) {
return "hello world";
}
}
練習
創建student的class setter ,getter
public class Student {
Integer id;
String name;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
@RestController
public class MyController4 {
@RequestMapping("/test")
public String test(@RequestBody Student student) {
System.out.println("student id: " + student.getId());
System.out.println("student name: " + student.getName());
return "hello world";
}
}
Talend API Tester
POST http://localhost:8080/test

request body
conolse:
Talend API Tester 測試2
POST http://localhost:8080/test
request body
200 成功
conolse:
多出來的參數會忽略 ,少帶的參數會給null