Postman是什么
在開發或者調試網絡程序或者是網頁B/S模式的程序的時候,需要一些方法來跟蹤網頁請求,用戶可以使用一些網絡的監視工具比如著名的Firebug等網頁調試工具。
網頁調試工具Postman不僅可以調試簡單的css、html、腳本等簡單的網頁基本信息,它還可以發送幾乎所有類型的HTTP請求。
Postman下載
1、下載谷歌瀏覽器,在拓展商店下載Postman並安裝
2、下載Windows32/64/Mac版安裝包,點開即用。
3、據說Linux下載后解壓,通過命令行即可執行。
Postman主界面詳解

測試GET請求
1、controller相關代碼,以查詢學生信息為例
//查詢並返回所有學生信息 @GetMapping("/AllStu") public Map<String,Object> index(){ Map<String,Object> modelMap=new HashMap<String,Object>(); List<Student> list = studentService.getAllStudent(); modelMap.put("stuList",list); return modelMap; }
2、Postman使用方法
選擇“GET”,地址欄輸入"localhost:8080"+controller定義的后綴,點擊"Send"發送Get請求。
請求到的數據將以JSON數組的形式,顯示在最下方,stuList就是put進去的標題。
測試POST請求
1、controller相關代碼,以添加學生為例
@PostMapping("/addStu")
public Map<String,Object> input(@RequestBody Student student){
Map<String,Object> modelMap =new HashMap<String,Object>();
modelMap.put("success",studentService.addStudent(student));
return modelMap;
}

2、使用Postman測試POST請求
選擇"POST",在地址欄輸入請求url。
點擊"Headers",如圖所示設定頭的數據類型,我這里是JSON形式的數據。

點擊"Body",選擇"raw",輸入JSON格式的數據測試返回的Map類型是否正確。

更復雜的使用方法,留待日后補充完善吧。
------------------------------------------------我是分割線--------------------------------------------------------------
之前已經測試過了增加和查詢學生數據功能,當時沒有繼續是因為刪除和修改沒有調試好。。
刪除功能測試
1、controller相關代碼
@GetMapping("/deleteStu/{id}")
public boolean delete(@PathVariable("id") Integer id){
try{
studentService.deleteStudent(id);
}
catch (Exception e){
return false;
}
return true;
}
2、使用Postman測試刪除

修改功能測試
1、controller相關代碼
/** * 修改學生信息 * @param id * return */ @GetMapping("/updateStu/{id}") public Map<String,Object> updateData(@PathVariable("id") Integer id){ Map<String,Object> map=new HashMap<String,Object>(); Optional<Student> student=studentService.findByID(id); map.put("student",student); return map; }
2、Postman測試修改功能
這個修改功能做的很迷,updateStu/{id}返回的是userID為{id}的學生信息,小程序里面做的修改按鈕就是添加功能,搞不懂這個JPA怎么定義的。。以后再說。

參考資料:
