后端如何接受前端传过来的Json数据

这里只讲解一些最常用的,最好用的方式

对于参数较少的,我们可以
使用@RequestParam

 @RequestMapping("/testJson2")
    @ResponseBody
    public String testJson2(@RequestParam String username,@RequestParam String password){
        System.out.println(username);//hahah
        System.out.println(password);//123456
        return "aaaa";
    }

对于参数较多的
同样也是使用@RequestParam,参数类型使用Map

     @RequestMapping("/modifyName")
    @ResponseBody
   
    public ResponseResult modify1(@RequestParam Map map) {
    	//这里获取什么数据拒绝于你前端传递的数据
         System.out.println( map.get("id"));   
        return ResponseResult.success();
    }

也可也使用对象进行接受,但是前提是参数必须保持一致。

 @RequestMapping("/modifyName")
    @ResponseBody
    public ResponseResult modify1(User user) {
    	 //直接用get set 获取你要的数据就行
        return ResponseResult.success();
    }
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 天前 )
960b763e 2 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐