1. 程式人生 > >【springmvc】傳值的幾種方式&&postman接口測試

【springmvc】傳值的幾種方式&&postman接口測試

red ews 參數 一點 名稱 each comment PQ 分享圖片

最近在用postman測試postman接口,對於springmvc傳值這一塊,測試了幾種常用方式,總結一下。對於postman這個工具的使用也增加了了解。postman測試很棒,有了工具,測試接口,事倍功半。


一、單個參數傳遞


1.@RequestBody註解

[java] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"> /**
  2. * 測試單個參數@RequestBody
  3. */
  4. @CrossOrigin
  5. @RequestMapping
    (value = {"/insertTestParamsRequest"}, method = RequestMethod.GET)
  6. @ResponseBody
  7. public void insertTestParamsRequest(@RequestBody String name, @RequestBody String age) {
  8. System.out.println("name=====" + name);
  9. System.out.println("age=====" + age);
  10. }
  11. </span>

測試請求路徑


2.@RequestParam

常用來處理簡單類型的綁定,通過Request.getParameter() 獲取的String可直接轉換為簡單類型的情況( ;
該註解有兩個屬性: value、required; value用來指定要傳入值的id名稱,required用來指示參數是否必須綁定;

[java] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"> /**
  2. * 測試單個參數@RequestParam
  3. */
  4. @CrossOrigin
  5. @RequestMapping(value = {"/insertTestParams"}, method = RequestMethod.GET)
  6. @ResponseBody
  7. public void insertTestParams(HttpServletRequest request, @RequestParam String name, @RequestParam String age) {
  8. System.out.println("name=====" + name);
  9. System.out.println("age=====" + age);
  10. }</span>

請求路徑:

技術分享圖片
3.@PathVariable註解

路徑為resultful風格,將參數當做請求路徑。

當使用@RequestMapping URI template 樣式映射時, 即 Url/{paramId}, 這時的paramId可通過 @Pathvariable註解綁定它傳過來的值到方法的參數上。

[java] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"> /**
  2. * 測試單個參數@PathVariable
  3. */
  4. @CrossOrigin
  5. @RequestMapping(value = {"/insertTest/{name}/{age}"}, method = RequestMethod.GET)
  6. @ResponseBody
  7. public void insertTestPathVeriable(HttpServletRequest request, @PathVariable("name") String name, @PathVariable String age) {
  8. System.out.println("name=====" + name);
  9. System.out.println("age=====" + age);
  10. }</span>

上面代碼把URI template 中變量 name的值和age的值,綁定到方法的參數上。若方法參數名稱和需要綁定的uri template中變量名稱不一致,需要在@PathVariable("name")指定uri template中的名稱。


二、傳遞pojo對象


1.@RequestBody註解

[java] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"> /*測試添加實體*/
  2. @CrossOrigin
  3. @RequestMapping(value = {"/insertEntityTest"}, method = RequestMethod.POST)
  4. @ResponseBody
  5. public void insertEntityTest(@RequestBody CurriculumScheduleEntity curriculumScheduleEntity) {
  6. System.out.println("name=====" + curriculumScheduleEntity.getClassId());
  7. System.out.println("age=====" + curriculumScheduleEntity.getTeachclassId());
  8. }</span>

postman通過json格式測試

技術分享圖片


2.直接寫實體

[java] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"> /*測試添加實體*/
  2. @CrossOrigin
  3. @RequestMapping(value = {"/insertTest"}, method = RequestMethod.POST)
  4. @ResponseBody
  5. public void insertTest(CurriculumScheduleEntity curriculumScheduleEntity) {
  6. System.out.println("name=====" + curriculumScheduleEntity.getClassId());
  7. System.out.println("age=====" + curriculumScheduleEntity.getWeekId());
  8. }</span>

form表單測試
[html] view plain copy print?
  1. <span style="font-family:‘KaiTi_GB2312‘;font-size:18px;"><div>
  2. <form action="/curriculumSchedule/insertTest" method="post">
  3. classId :<input name="classId"><br>
  4. teachClassId:<input name="weekId"><br>
  5. <input type="submit" value="提交">
  6. </form>
  7. </div></span>

postman測試格式

技術分享圖片


三、postman測試List類型參數

以List<string>為例,測試批量刪除方法,參數為List<String>。寫這個其實沒有什麽技術,但是中午在測試List接口的時候,用postman測試,格式一致寫錯,不知道用postman該怎麽測試了。所以花費了一點時間,記錄下來,思考這個工具的執行過程。

技術分享圖片

Controller方法

技術分享圖片

3.參考連接

http://www.cnblogs.com/sandyliu1999/p/4802706.html



【springmvc】傳值的幾種方式&&postman接口測試