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

猜你喜欢

转载自blog.csdn.net/weixin_43424325/article/details/127560024
今日推荐