美文网首页
Controller接收前端参数的几种方式总结

Controller接收前端参数的几种方式总结

作者: 寒岳旺旺 | 来源:发表于2020-04-26 16:57 被阅读0次

    一、非JSON方式接收请求参数

    1、普通方式-请求参数名和Controller方法的参数一致

    • Controller代码实现
    @RestController
    @RequestMapping("/param")
    @Slf4j
    public class TestParamsController {
        @GetMapping("/add")
        public String addEmployee(String lastName,String email){
            return "lastName:"+lastName+",email:"+email;
        }
    }
    
    • 测试展示

    http://127.0.0.1:8080/param/add?lastName=旺旺&email=33453432@qq.com

    image

    2、对象方式-请求参数名和Controller方法中的对象的参数一致

    • Employee实体类
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Employee {
        private Integer id;
        private String lastName;
        private Integer gender;
        private String email;
        private Integer dId;
    }
    
    • Controller代码实现
    @RestController
    @RequestMapping("/test")
    @Slf4j
    public class ParamsController {
        @GetMapping("/employee")
        public String addEmployee(Employee employee){
            return "lastName:"+employee.getLastName()+",email:"+employee.getEmail();
        }
    }
    
    • 测试展示

    http://127.0.0.1:8080/test/employee?lastName=战三&email=33453432@qq.com

    image

    3、自定义方法参数名-当请求参数名与方法参数名不一致时

    注意可以在参数中增加@RequestParam注解。如果在方法中的参数增加了该注解,说明请求的url必须带该带有该参数,否则不能执行该方法。如果在方法中的参数没有增加该注解,说明请求的url无需带有该参数,也能继续执行该方法。

    • @RequestParam(defaultValue="0")可设置默认值(仅当传入参数为空时)。

    • @RequestParam(value="id")可接受传入参数为id的值,覆盖该注解注释的字段。

    • @RequestParam(name="name",defaultValue = "李四") String u_name 如果传入字段”name”为空,默认u_name的值为”李四”。若传入”name”不为空,默认u_name值为传入值。

    • 正常入参,两个参数都必传,且@RequestParam不写别名则代表该参数必传

    @RestController
    @RequestMapping("/office")
    @Slf4j
    public class TestController {
        @GetMapping("/deport")
        public String addEmployee(@RequestParam("name") String lastName,@RequestParam String email){
            return "lastName:"+lastName+",email:"+email;
        }
    }
    
    • 正确请求

    http://127.0.0.1:8080/office/deport?name=张三&email=34453432@qq.com

    image
    • 请求入参不用name代替lastName

    http://127.0.0.1:8080/office/deport?lastName=张三&email=34453432@qq.com

    报错:

    image
    • 必传参数不传

    http://127.0.0.1:8080/office/deport?name=张三

    报错:

    image

    4、@PathVariable获取路径中的参数接收

    • Controller代码实现
    public class EmpController{
    
        @Autowired
        EmployeeMapper employeeMapper;
    
        @GetMapping("/emp/{id}")
        public Employee getEmpById(@PathVariable("id") Integer id){
            log.info("查询顾客");
            return employeeMapper.getEmpById(id);
        }
    
    • 测试请求

    http://127.0.0.1:8080/emp/1

    image

    二、JSON格式接收请求入参

    1、JSON格式接收普通对象

    • Cotroller代码实现
    @RestController
    @RequestMapping("/resp")
    @Slf4j
    public class ParamsRespController {
        @PostMapping("/test")
        public String addEmployee(@RequestBody Employee employee){
            return "lastName:"+employee.getLastName()+",email:"+employee.getEmail();
        }
    }
    
    • 测试请求

    http://127.0.0.1:8080/resp/test

    image

    2、JSON格式接收List对象

    • Controller代码实现
    @RestController
    @RequestMapping("/resplist")
    @Slf4j
    public class ParamsRespListController {
        @PostMapping("/testlist")
        public String addEmployee(@RequestBody List<Employee> employees){
            StringBuilder sb = new StringBuilder("{");
            if( null != employees){
                for(Employee employee:employees){
                    sb.append("{"+"lastName:"+employee.getLastName()+",email:"+employee.getEmail()+"}");
                }
            }
            sb.append("}");
            return sb.toString();
    
        }
    }
    
    
    • 测试请求

    http://127.0.0.1:8080/resplist/testlist

    image

    3、JSON格式接收Map对象

    • Controller代码实现
    @RestController
    @RequestMapping("/respmap")
    @Slf4j
    public class ParamsRespMapController {
        @PostMapping("/testmap")
        public String addEmployee(@RequestBody Map<String,Employee> employees){
            StringBuilder sb = new StringBuilder("[");
            if( null != employees){
                for(Map.Entry<String,Employee> employeeEntry : employees.entrySet()){
                    sb.append(employeeEntry.getKey()+":"+employeeEntry.getValue()+",");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }
    
    • 测试请求

    http://127.0.0.1:8080/respmap/testmap

    image

    相关文章

      网友评论

          本文标题:Controller接收前端参数的几种方式总结

          本文链接:https://www.haomeiwen.com/subject/awljwhtx.html