Springmvc introduces how to receive json objects:
1. Receive in entity type
Frontend ajax submits data:
Function fAddObj () {var obj ={}; obj ['objname'] = "obj"; obj ['pid '] = 1; $. ajax ({url: 'admin/Obj/addObj. do ', method: 'post', contentType: 'application/json', // The error 415 is not added: Unsupported Media Type data: json. stringify (obj), // transmit success: function (data) {console. log ("success... ") ;}, error: function (data) {console. log ("error... ");}});}
Springmvc receives the following in the form of a model object:
@ Controller @ RequestMapping ("/admin/Obj") public class ObjAction {// injection operation class @ Autowired private ObjService objService; @ RequestMapping (value = "/addObj ") @ ResponseBody public String addObj (@ RequestBody Obj obj) {this. objService. insertObj (cate); return "success ";}}
2. Receive with Map
@ Controller @ RequestMapping ("/admin/Obj") public class ObjAction {/*** frontend operations are the same as above * @ return */@ RequestMapping (value = "/updateAttr ") @ ResponseBody public String updateAttr (@ RequestBody Map <String, String> map) {if (map. containsKey ("id") {Integer id = Integer. parseInt (map. get ("id");} if (map. containsKey ("objname") {String objname = map. get ("objname "). toString ();} if (map. containsKey ("pid") {Integer pid = Integer. parseInt (map. get ("pid");} // operation... return "success ";}}