今天在做react 前后端請(qǐng)求的時(shí)候骨饿,發(fā)現(xiàn)有個(gè)@RequestBody 不能獲取map ,查閱了一下get/post請(qǐng)求台腥,記錄一下宏赘,方便查閱。
一黎侈、POST請(qǐng)求
1察署、 @RequestParam方式:
前端,需要用qs.stringify(data):
ceshi = ()=>{
let data = {
// "idd": "f6588b4d3a274d599c8696e3a2e89579",
// "name":"水"
idd: 'Fred',
lastName: 'Flintstone'
};
// alert()
axios.post(`${base}/t-stu/testWarpper`,
qs.stringify({
idd: 'f6588b4d3a274d599c8696e3a2e89579',
lastName: 'Flintstone'
})
)
.then((response)=> {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
}
后臺(tái)@RequestParam 獲取參數(shù):
@ResponseBody
@RequestMapping(value="/testWarpper",method = RequestMethod.POST)
public Map<String,Object> testWarpper(@RequestParam("idd") String sid){
Map<String,Object> map = new HashMap<>();
QueryWrapper<TStu> queryWrapper = new QueryWrapper<>();
// queryWrapper.eq("stuid",paramMap.get("idd").toString());
queryWrapper.eq("stuid",sid);
// queryWrapper
List<TStu> list = tStuMapper.selectList(queryWrapper);
map.put("result",list);
return map;
}
2峻汉、@RequestBody方式
前端代碼:
ceshi = ()=>{
let data = {
// "idd": "f6588b4d3a274d599c8696e3a2e89579",
// "name":"水"
idd: 'f6588b4d3a274d599c8696e3a2e89579',
lastName: 'Flintstone'
};
// alert()
axios.post(`${base}/t-stu/testWarpper`, data
)
.then((response)=> {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
}
后臺(tái)代碼:
@ResponseBody
@RequestMapping(value="/testWarpper",method = RequestMethod.POST)
public Map<String,Object> testWarpper(@RequestBody(required = false) Map paramMap){
Map<String,Object> map = new HashMap<>();
QueryWrapper<TStu> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("stuid",paramMap.get("idd").toString());
// queryWrapper.eq("stuid",sid);
// queryWrapper
List<TStu> list = tStuMapper.selectList(queryWrapper);
map.put("result",list);
return map;
}
二贴汪、GET請(qǐng)求的方式
1.@RequestParam方式
前端:
ceshi = ()=>{
let data = {
// "idd": "f6588b4d3a274d599c8696e3a2e89579",
// "name":"水"
idd: 'f6588b4d3a274d599c8696e3a2e89579',
lastName: 'Flintstone'
};
// alert()
axios.get(`${base}/t-stu/testWarpper`,{params:data}
)
.then((response)=> {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
}
后臺(tái):
@ResponseBody
@RequestMapping(value="/testWarpper",method = RequestMethod.GET)
public Map<String,Object> testWarpper(@RequestParam("idd") String sid){
Map<String,Object> map = new HashMap<>();
QueryWrapper<TStu> queryWrapper = new QueryWrapper<>();
// queryWrapper.eq("stuid",paramMap.get("idd").toString());
queryWrapper.eq("stuid",sid);
// queryWrapper
List<TStu> list = tStuMapper.selectList(queryWrapper);
map.put("result",list);
return map;
}
}
2.@ModelAttribute方式,用于接收多個(gè)參數(shù)的dto實(shí)體
前端:(react 有做代理處理)
后臺(tái):
3.@SpringQueryMap 方式
/**
* 工單列表
*
* @param dto
* @return Result<PageVO < WorkOrderVO>>
*/
@GetMapping("/workOrder/list")
@ApiOperation("工單列表")
public Result<PageVO<WorkOrderVO>> queryList(@Valid @SpringQueryMap WorkOrderQueryDTO dto) {
return Result.buildSuccess(ResultCode.SUCCESS, workOrderService.queryList(dto));
}
postman測(cè)試
debug結(jié)果
總結(jié):
測(cè)試了axios.get不能用@RequestBody獲取休吠,能用@RequestParam和@SpringQueryMap獲取扳埂。axios.post 可以用@RequestBody獲取且不需要qs.stringify序列化,直接傳對(duì)象就可以了瘤礁。post請(qǐng)求后臺(tái)用@RequestParam()獲取的時(shí)候阳懂,前端需要用qs.stringify序列化所傳遞的參數(shù)