输入
前台代码:
var cfg = { type: 'POST', data: JSON.stringify({userName:'winzip',password:'password',mobileNO:'13818881888'}), dataType: 'json', contentType:'application/json;charset=UTF-8', success: function(result) { alert(result.success); } };function doTestJson(actionName){ cfg.url = actionName; $.ajax(cfg);}
后台代码:
1、使用@RequestBody来设置输入
@RequestMapping("/json1")@ResponseBodypublic JsonResult testJson1(@RequestBody User u){ log.info("get json input from request body annotation"); log.info(u.getUserName()); return new JsonResult(true,"return ok");}
2、使用HttpEntity来实现输入绑定
@RequestMapping("/json2") public ResponseEntitytestJson2(HttpEntity u){ log.info("get json input from HttpEntity annotation"); log.info(u.getBody().getUserName()); ResponseEntity responseResult = new ResponseEntity ( new JsonResult(true,"return ok"),HttpStatus.OK); return responseResult;}
输出
1:使用@responseBody来设置输出内容为context body (有方法注解/返回参数注解)
2:返回值设置为ResponseEntity<?>类型,以返回context body
启用