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
由于傳遞過來的數(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)變成空格的問題
- SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過來的多個(gè)參數(shù)的兩種方式
- 解讀SpringBoot接收List<Bean>參數(shù)問題(POST請(qǐng)求方式)
- SpringBoot請(qǐng)求參數(shù)接收方式
- SpringBoot2之PUT請(qǐng)求接收不了參數(shù)的解決方案
- springboot如何接收get和post請(qǐng)求參數(shù)
- SpringBoot請(qǐng)求參數(shù)傳遞與接收說明小結(jié)
- SpringBoot優(yōu)雅接收前端請(qǐng)求參數(shù)的詳細(xì)過程
- SpringBoot接收請(qǐng)求參數(shù)的四種方式總結(jié)
相關(guān)文章
Java8內(nèi)存模型PermGen Metaspace實(shí)例解析
這篇文章主要介紹了Java8內(nèi)存模型PermGen Metaspace實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java并發(fā)編程回環(huán)屏障CyclicBarrier
這篇文章主要介紹了Java并發(fā)編程回環(huán)屏障CyclicBarrier,文章繼續(xù)上文所介紹的Java并發(fā)編程同步器CountDownLatch展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04
在JSP頁(yè)面內(nèi)編寫java代碼方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于在JSP頁(yè)面內(nèi)編寫java代碼方法和步驟,有需要的朋友們學(xué)習(xí)下。2019-01-01
Springboot通用mapper和mybatis-generator代碼示例
這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
JPA原生SQL實(shí)現(xiàn)增刪改查的示例詳解
Java從JDK源碼角度對(duì)Object進(jìn)行實(shí)例分析

