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


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


一、單個參數傳遞


1.@RequestBody注解

  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用來指示參數是否必須綁定;

  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注解綁定它傳過來的值到方法的參數上。

  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注解

  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.直接寫實體

  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表單測試
  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



2018_05_24更新(6.0版本)

1、如果要傳遞多個參數其中一個Long類型的數組,另外一個是Long類型的數據,還有一個實體,那么后者可以直接傳,前者的話用Body中的formData來進行傳遞就好了,具體如下圖:

![](https://images2018.cnblogs.com/blog/1112483/201805/1112483-20180524171849503-376268906.png)

2、在上面提到傳遞實體接收的方式,我試了不行不過可以直接將實體屬性按照表單那樣去寫就可以獲取到了,如下圖所示:

![](https://images2018.cnblogs.com/blog/1112483/201805/1112483-20180524175136404-560497635.png) 小技巧:可以直接復制JSON中的選項,然后將光標點擊在“x-www-form-ulencoded”第一項最后Ctrl+V就可以直接粘貼了非常方便。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM