axios post/get请求 @RequestBody @RequestParam对应的用法

7 篇文章 1 订阅

参考文章

一、POST请求
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
      });  
    }

注:qs安装:
npm install --save axios vue-axios qs
import qs from ‘qs’ qs 用来解决vue中post请求以 a=a&b=b 的格式(详情)
后台@RequestParam 获取参数:

@Autowired private VideoService videoService;
	@ResponseBody
	@RequestMapping(value = "getVideoPath",produces={"text/json;charset=UTF-8","application/json;charset=UTF-8"})
	public ResultMsg getVideoPath(@RequestParam(value="glb") String glb,@RequestParam(value="railwayName") String railwayName, @RequestParam(value="upDown") String upDown) {
		int glbInt = (Integer.parseInt(glb)/100)*100;
		BigDecimal glBigDecimal = new BigDecimal(glbInt);
		List<String> videoInfo = videoService.getVideoPathAndSecond(railwayName, glBigDecimal, upDown);
		if (videoInfo.size() > 0) {
			ResultMsg resultMsg = new ResultMsg(0, "ok", videoInfo);
		    return resultMsg;
		}else {
			ResultMsg resultMsg = new ResultMsg(1, "false", null);
		    return resultMsg;
		}
			
	}

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
      });  
    }

后台代码:

@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请求的方式
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
      });  
    }

后台:

@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;
    }
}

总结:
测试了axios.get不能用@RequestBody获取,能用@RequestParam获取。axios.post 可以用@RequestBody获取且不需要qs.stringify序列化,直接传对象就可以了。post请求后台用@RequestParam()获取的时候,前端需要用qs.stringify序列化所传递的参数

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ponGISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值