Postman工具的使用?
前后端分離開發(fā)的情況下每開發(fā)一個功能都要對接口進(jìn)行測試,通常都會去瀏覽器訪問測試,但是這樣子都是get請求,測試不了post請求。
postman可以模擬瀏覽器發(fā)起任何形式的http請求 ,下載完postman后登陸進(jìn)去
創(chuàng)建一個workspace
?
簡單參數(shù)
?原始方式
這部分在Javaweb——Request通用方式獲取請求參數(shù)_北嶺山腳鼠鼠的博客-CSDN博客
?新建一個Controller,定義好一個資源用于接收
?
測試輸出如下?
springboot方式??
將需要的參數(shù)名稱直接寫到參數(shù)的位置進(jìn)行接收
如果是使用post方式進(jìn)行傳參就要在請求體當(dāng)中攜帶兩個參數(shù)
如果參數(shù)名稱對應(yīng)不上會變成空,要想使用不同的名字需要用到一個注解
@RequestParam?
?注意點(diǎn):
?文章來源地址http://www.zghlxwxcb.cn/news/detail-781482.html
//springboot方式
@RequestMapping("/simpleParam")
public String springbootParam(@RequestParam(name="name" ,required = false) String username, Integer age){
//h獲取請求參數(shù)
System.out.println(username+":"+age);
return "接收成功";
}
實(shí)體參數(shù)
?要想讓封裝成功,請求的參數(shù)名和pojo里面的屬性名必須相同
@RequestMapping("/simplePojo")
public String springbootParam(User user){
//h獲取請求參數(shù)
System.out.println(user.toString());
return "接收成功";
}
輸出如下
復(fù)雜實(shí)體對象?
?方法差不多,不再演示
?數(shù)組集合參數(shù)?
在一些場景,需要傳遞的參數(shù)個數(shù)并不是固定的,比如可復(fù)選表單,這種就需要傳一個集合進(jìn)去
數(shù)組參數(shù)??
接收方式
@RequestMapping("/array")
public String arrayParam(String[] hobby){
//h獲取請求參數(shù)
System.out.println(Arrays.toString(hobby));
return "接收成功";
}
?
?
?集合參數(shù)
@RequestMapping("/listParam")
public String arrayParam(@RequestParam List<String> hobby){
//h獲取請求參數(shù)
System.out.println(hobby);
return "接收成功";
}
?
日期參數(shù)?
?前端傳遞的日期可能有多種樣式,需要在后端用@DateTimeFormat指定日期參數(shù)的格式
@RequestMapping("/dateParam")
public String arrayParam(@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){
//h獲取請求參數(shù)
System.out.println(updateTime);
return "接收成功";
}
JSON參數(shù)?
JSON格式的請求參數(shù)需要放到請求體當(dāng)中,所以必須使用post方式傳參
使用@RequestBody注解將json格式的請求數(shù)據(jù)封裝到一個實(shí)體對象里面?
后端接收
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
//h獲取請求參數(shù)
System.out.println(user);
return "接收成功";
}
?
?路徑參數(shù)
用到一個新的注解@PathVariable把路徑參數(shù)的值綁定給方法形參
?
?輸出如下
多個路徑參數(shù)獲取?
?用法和上面一樣,不再演示
文章來源:http://www.zghlxwxcb.cn/news/detail-781482.html
?
到了這里,關(guān)于SpringBoot——請求-Postman工具-6種常見請求參數(shù)接收的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!