请求参数接收说明

Get请求

直接名称匹配

    @GetMapping("/test")
    public void test(String name){
        System.out.println(name);
    }

@RequestParam

注:name的别名是value,value的别名是name,即两者等价,不能同时存在

    @GetMapping("/test")
    public void test(@RequestParam(value = "name", required = false) String name){
        System.out.println(name);
    }

HttpServletRequest

    @GetMapping("/test")
    public void test(HttpServletRequest request){
        System.out.println(request.getParameter("name"));
    }

 

Post请求

实体类

    @PostMapping("/test")
    public void test(@RequestBody User user){
        System.out.println(user.getAge());
    }

Map

    @PostMapping("/test")
    public void test(@RequestBody Map<String,String> params){
        System.out.println(params.get("age"));
    }

使用 String 类型接收 post 请求参数

    @PostMapping("/test")
    public void test(@RequestBody String params){
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        System.out.println(paramsJSONObject.getIntValue("age"));
    }

 

posted @ 2024-12-25 14:58  先娶国王后取经  阅读(18)  评论(0)    收藏  举报