真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java中Spring接收web請(qǐng)求參數(shù)的方式有哪些

小編給大家分享一下java中Spring接收web請(qǐng)求參數(shù)的方式有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出城口免費(fèi)做網(wǎng)站回饋大家。

1 查詢參數(shù)

請(qǐng)求格式:url?參數(shù)1=值1&參數(shù)2=值2...
同時(shí)適用于GET和POST方式
spring處理查詢參數(shù)的方法又有幾種寫法:

方法一:
方法參數(shù)名即為請(qǐng)求參數(shù)名

  // 查詢參數(shù)1
  @RequestMapping(value = "/test/query1", method = RequestMethod.GET)  
  public String testQuery1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法二:
從HttpServletRequest中提取參數(shù)

  // 查詢參數(shù)2
  @RequestMapping(value = "/test/query2", method = RequestMethod.GET)  
  public String testQuery2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法三:
方法參數(shù)名和請(qǐng)求參數(shù)名可以不一樣,通過@RequestParam注解來綁定參數(shù)

  // 查詢參數(shù)3
  @RequestMapping(value = "/test/query3", method = RequestMethod.GET)  
  public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);    
    return "username=" + un + ", password=" + pw;
  }

方法四:
創(chuàng)建一個(gè)實(shí)體類對(duì)象作為參數(shù)承載體,spring會(huì)根據(jù)參數(shù)名稱自動(dòng)將參數(shù)綁定到實(shí)體類對(duì)象的屬性上

  // 查詢參數(shù)4
  @RequestMapping(value = "/test/query4", method = RequestMethod.GET)  
  public String testQuery4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

實(shí)體類定義如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builderpublic class User {  
   private String username;  
   private String password;
}

這里用到了第三方庫(kù)lombok,這樣就不需要在代碼中手動(dòng)添加get、set等方法,lombok會(huì)自動(dòng)添加。

發(fā)送請(qǐng)求的curl命令如下:

curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'

交互報(bào)文如下:

GET /test/query1?username=aaa&password=bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:01:30 GMT

username=aaa, password=bbb

2 表單參數(shù)

請(qǐng)求參數(shù)不在url中,而是在Body體中,格式為:url?參數(shù)1=值1&參數(shù)2=值2...

適用于POST方式

表單參數(shù)處理方法和前面的請(qǐng)求參數(shù)處理方法幾乎完全一樣,只是RequestMethod注解中將method方法設(shè)置成POST方法

方法一:

  // 表單參數(shù)1
  @RequestMapping(value = "/test/form1", method = RequestMethod.POST)  
  public String testForm1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法二:

  // 表單參數(shù)2
  @RequestMapping(value = "/test/form2", method = RequestMethod.POST)  
  public String testForm2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法三:

  // 表單參數(shù)3
  @RequestMapping(value = "/test/form3", method = RequestMethod.POST)  
  public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);    
    return "username=" + un + ", password=" + pw;
  }

方法四:

  // 表單參數(shù)4
  @RequestMapping(value = "/test/form4", method = RequestMethod.POST)
  public String testForm4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

curl請(qǐng)求命令如下:

curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1

請(qǐng)求和響應(yīng)報(bào)文如下:

POST /test/form1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

username=aaa&password=bbbHTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:05:35 GMT

username=aaa, password=bbb

3 路徑參數(shù)

請(qǐng)求參數(shù)為url中的一部分,格式為:url/參數(shù)1/參數(shù)2...
同時(shí)適用于GET和POST方式
代碼如下:

  @RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)  
  public String testUrl(@PathVariable String username, @PathVariable String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

請(qǐng)求curl命令如下:

curl -i http://192.168.1.14:8080/test/url/aaa/bbb

請(qǐng)求和響應(yīng)報(bào)文如下:

GET /test/url/aaa/bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:07:44 GMT

username=aaa, password=bbb

4 json格式參數(shù)

請(qǐng)求參數(shù)在Body體中,并且為json格式。需要添加請(qǐng)求頭:Content-Type: application/json;charset=UTF-8
適用于POST方式
方法一:
定義實(shí)體類,將json對(duì)象解析成實(shí)力類,需要添加RequestBody注解

  // json參數(shù)1
  @RequestMapping(value = "/test/json1", method = RequestMethod.POST)  
  public String testJson1(@RequestBody User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法二:

如果不像定義實(shí)體類,也可以將json請(qǐng)求直接解析成JSONObject類

  // json參數(shù)2
  @RequestMapping(value = "/test/json2", method = RequestMethod.POST)  
  public String testJson2(@RequestBody JSONObject json) {
    String username = json.getString("username");
    String password = json.getString("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

方法三:

也可以將json對(duì)象直接解析成Map對(duì)象

  // json參數(shù)3
  @RequestMapping(value = "/test/json3", method = RequestMethod.POST)  
  public String testJson3(@RequestBody Map userMap) {
    String username = userMap.get("username");
    String password = userMap.get("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

請(qǐng)求curl命令如下:

curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '{    
   "username" : "aaa",    
   "password" : "bbb"
} 
'http://192.168.1.14:8080/test/json1

請(qǐng)求和響應(yīng)報(bào)文如下:

POST /test/json1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json;charset=UTF-8
Content-Length: 52


{
    "username" : "aaa",
    "password" : "bbb"
}HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:09:06 GMT

username=aaa, password=bbb

以上是java中Spring接收web請(qǐng)求參數(shù)的方式有哪些的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


本文標(biāo)題:java中Spring接收web請(qǐng)求參數(shù)的方式有哪些
標(biāo)題URL:http://weahome.cn/article/ijsejs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部