SpringBoot如何接收Post請求Body里面的參數(shù)
如何接收Post請求Body里的參數(shù)
ApiPost測試數(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)建好對應(yīng)的Bean
由于傳遞過來的數(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 作用是將請求體中的Json字符串自動接收并且封裝為實(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獲取請求參數(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; ? ? } } ?
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot接收http請求,解決參數(shù)中+號變成空格的問題
- SpringBoot用實(shí)體接收Get請求傳遞過來的多個參數(shù)的兩種方式
- 解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)
- SpringBoot請求參數(shù)接收方式
- SpringBoot2之PUT請求接收不了參數(shù)的解決方案
- springboot如何接收get和post請求參數(shù)
- SpringBoot請求參數(shù)傳遞與接收說明小結(jié)
- SpringBoot優(yōu)雅接收前端請求參數(shù)的詳細(xì)過程
- SpringBoot接收請求參數(shù)的四種方式總結(jié)
相關(guān)文章
Java8內(nèi)存模型PermGen Metaspace實(shí)例解析
這篇文章主要介紹了Java8內(nèi)存模型PermGen Metaspace實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03Java并發(fā)編程回環(huán)屏障CyclicBarrier
這篇文章主要介紹了Java并發(fā)編程回環(huán)屏障CyclicBarrier,文章繼續(xù)上文所介紹的Java并發(fā)編程同步器CountDownLatch展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04Springboot通用mapper和mybatis-generator代碼示例
這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

JPA原生SQL實(shí)現(xiàn)增刪改查的示例詳解

Java從JDK源碼角度對Object進(jìn)行實(shí)例分析