前言:
现在姐妹们对“后端接收json对象”大概比较重视,看官们都需要剖析一些“后端接收json对象”的相关资讯。那么小编也在网摘上搜集了一些对于“后端接收json对象””的相关知识,希望我们能喜欢,兄弟们快快来了解一下吧!作者 | 丁昆
来源 | urlify.cn/I3EvUn
一、Json对象+@RequestBody接收
var val = {id: 1, name: "小明"};$.ajax({ url: "/getJson", dataType: "JSON", type: "post", contentType: 'application/json;charset=UTF-8', data: JSON.stringify(val), success: function (msg) { console.log(msg) }})
后端获取参数:Map<String,Object>
@PostMapping("/getJson")@ResponseBodypublic Map<String,Object> getJsonVal(@RequestBody Map<String,Object> user) { System.out.println("user = " + user.get("id")); System.out.println("user = " + user.get("name")); return user;}
后端获取参数:对象
@PostMapping("/getJson")@ResponseBodypublic User getJsonVal(@RequestBody User user) { return user;}二、传JSON对象#
var val = {"id": 1, "name": "小明"};$.ajax({ url: "/getJson", dataType: "JSON", type: "post", // contentType: 'application/json;charset=UTF-8', //不能加 data: val, success: function (msg) { console.log(msg) }})
后端获取参数
@PostMapping("/getJson")@ResponseBodypublic User getJsonVal(@RequestParam("id") String id,@RequestParam("name") String name) { User user = new User(); user.setId(Integer.parseInt(id)); user.setName(name); return user;}三、json集合+@RequestBody接收#
var val = [{"id": 1, "name": "小明"},{"id": 2, "name": "小红"}];$.ajax({ url: "/getJson", dataType: "JSON", type: "post", contentType: 'application/json;charset=UTF-8', //不能加 data: JSON.stringify(val), success: function (msg) { console.log(msg) }})
后端获取参数
@PostMapping("/getJson")@ResponseBodypublic List<User> getJsonVal(@RequestBody List<User> user) throws IOException { for(User user2 : user){ System.out.println("user2 = " + user2); } return user;}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #后端接收json对象