GET POST参数接收

1.GET POST通用

路径传参
eg:

前端传递
http://localhost:8080/admin/system/sysRole/1/2
后端@PathVariable接收
http://localhost:8080/admin/system/sysRole/{page}/{limit}
@PathVariable("page") Long page,
@PathVariable("limit") Long limit,

GET params传参

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'GET',
    params: params
  })
}

后台接收方式

  1. 不使用注解

    @PostMapping("/test")
    public Result test(Long id, String name) {
        return Res.ok();
    }
    
  2. 使用注解@RequestParam

    @RequestParam(“id”)中最好填写要接受的字段。

    @PostMapping("/test")
    public Result test(
    	@RequestParam("id") Long id, 
    	@RequestParam("name") String name) {
        return Res.ok();
    }
    
  3. 实体类接收
    对于情况1,2如果param中的参数过多,就会导致后端接收函数的参数过多,书写不便,这是可以使用实体类接收

    class TestClass {
    	Long id, 
    	String name
    }
    @PostMapping("/test")
    	public Result test(TestClass test) {
    	    return Res.ok();
    	}
    

Post请求体传参

@RequestBody接收

	// 实体类
	@Data
	public class TestEntity {
	    Long id;
	    String name;
	}
	
	// method
	const params = {
	    id: '123456789',
	    name: '张三'
	}
	test(params)
	
	// api
	export function test (params) {
	  return axios({
	    url: url,
	    method: 'POST', 
	    data: params
	  })
	}
	
	@PostMapping("/test")
	public Result test(@RequestBody TestEntity testEntity) {
	    return Res.ok();
	}

接收列表元素

  1. 使用params传递列表

    	
    	// method
    	const list= [a,b,c,d]
    	test(params)
    	
    	// api
    	export function test (list) {
    	  return axios({
    	    url: url,
    	    method: 'GET', 
    	    params: list
    	  })
    	}
    	
    	// 后台
    	@PostMapping("/test")
    	public Result test(@RequestParam("list") List<泛型> list) {
    	    return Res.ok();
    	}
    	
    	```
    
  2. 使用data传递列表

    @DeleteMapping("batchDeleteById")
        public Result batchDeleteById(@RequestBody List<String> ids){
            boolean remove = sysRoleService.removeByIds(ids);
            return remove ? Result.ok() : Result.fail();
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值