SpringBoot如何接收Post請(qǐng)求Body里面的參數(shù)
如何接收Post請(qǐng)求Body里的參數(shù)
ApiPost測(cè)試數(shù)據(jù)
{
? ? "list": [
? ? ? ? "{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
? ? ? ? "{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
? ? ? ? "{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
? ? ? ? "{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}"
? ? ],
? ? "type": 1
}Java接收數(shù)據(jù)
需要提前創(chuàng)建好對(duì)應(yīng)的Bean
由于傳遞過(guò)來(lái)的數(shù)據(jù)是String類型,因此需要轉(zhuǎn)換一步
import cn.hutool.json.JSONObject;
@PostMapping("/data/callback")
? ? public Object testResponse(
? ? ? ? ? ? @RequestBody JSONObject jsonObject
? ? ) {
? ? ? ? JSONArray jsonList = jsonObject.getJSONArray("list");
? ? ? ? ArrayList<DataEntity> list = new ArrayList<>();
? ? ? ? for (Object jsObject : jsonList){
? ? ? ? ? ? DataEntity dataEntity = JSONObject.parseObject(jsObject.toString(), DataEntity.class);
? ? ? ? ? ? list.add(dataEntity);
? ? ? ? }
? ? ? ? Integer type = (Integer) jsonObject.get("type");
? ? ? ? log.info(String.format("本次共接收%d條數(shù)據(jù),type=%d",list.size(),type));
? ? ? ? for (DataEntity dataEntity : list) {
? ? ? ? ? ? log.info(dataEntity.toString());
? ? ? ? }
? ? } ? ?SpringBoot獲取參數(shù)常用方式
參數(shù)在body體中
在方法形參列表中添加@RequestBody注解
@RequestBody 作用是將請(qǐng)求體中的Json字符串自動(dòng)接收并且封裝為實(shí)體。如下:
@PostMapping("/queryCityEntityById")
public Object queryCityEntityById(@RequestBody CityEntity cityEntity)
{
? ? return ResultUtil.returnSuccess(cityService.queryCityById(cityEntity.getId()));
}PathVaribale獲取url路徑的數(shù)據(jù)
如下:
@RestController
public class HelloController {
? ? @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
? ? public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
? ? ? ? return "id:"+id+" name:"+name;
? ? }
}RequestParam獲取請(qǐng)求參數(shù)的值
獲取url參數(shù)值,默認(rèn)方式,需要方法參數(shù)名稱和url參數(shù)保持一致
localhost:8080/hello?id=1000,如下:
@RestController
public class HelloController {
? ? @RequestMapping(value="/hello",method= RequestMethod.GET)
? ? public String sayHello(@RequestParam Integer id){
? ? ? ? return "id:"+id;
? ? }
}
?以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot接收http請(qǐng)求,解決參數(shù)中+號(hào)變成空格的問(wèn)題
- SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過(guò)來(lái)的多個(gè)參數(shù)的兩種方式
- 解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式)
- SpringBoot請(qǐng)求參數(shù)接收方式
- SpringBoot2之PUT請(qǐng)求接收不了參數(shù)的解決方案
- springboot如何接收get和post請(qǐng)求參數(shù)
- SpringBoot請(qǐng)求參數(shù)傳遞與接收說(shuō)明小結(jié)
- SpringBoot優(yōu)雅接收前端請(qǐng)求參數(shù)的詳細(xì)過(guò)程
- SpringBoot接收請(qǐng)求參數(shù)的四種方式總結(jié)
相關(guān)文章
tk.mybatis實(shí)現(xiàn)uuid主鍵生成的示例代碼
本文主要介紹了tk.mybatis實(shí)現(xiàn)uuid主鍵生成的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
javafx 如何將項(xiàng)目打包為 Windows 的可執(zhí)行文件exe
文章介紹了三種將JavaFX項(xiàng)目打包為.exe文件的方法:方法1使用jpackage(適用于JDK14及以上版本),方法2使用Launch4j(適用于所有JDK版本),方法3使用InnoSetup(用于創(chuàng)建安裝包),每種方法都有其特點(diǎn)和適用范圍,可以根據(jù)項(xiàng)目需求選擇合適的方法,感興趣的朋友一起看看吧2025-01-01
關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架)
這篇文章主要介紹了關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java分頁(yè)查詢--分頁(yè)顯示(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Java分頁(yè)查詢--分頁(yè)顯示(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
eclipse實(shí)現(xiàn)Schnorr數(shù)字簽名
這篇文章主要為大家詳細(xì)介紹了eclipse實(shí)現(xiàn)Schnorr數(shù)字簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot+Jpa多數(shù)據(jù)源配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the J
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the Java Runtime (class file version 54.0)2021-04-04
Spring使用三級(jí)緩存解決循環(huán)依賴的問(wèn)題
本文給大家分享Spring使用三級(jí)緩存解決循環(huán)依賴的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06

