SpringMVC @RequestBody processing Ajax requests
来源:互联网 发布:armin van buuren知乎 编辑:程序博客网 时间:2024/06/08 19:59
1 problem description
Recently in the debugging code and found the following problems:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null').
The front is a Ajax request, the data is a JSON object.
Background found the wrong is generally JSON data format transfer problems.
The object passed to the Java terminal 2
In the SpringMVC environment, @RequestBody receiver is a Json string object, not a Json object. However, in the Ajax request often pass are Json objects, and later found withJSON.stringify(data)The way can the object into a string. At the same time the Ajax request to the specifieddataType: "json",contentType:"application/json"This can easily be an object to the Java end, use @RequestBody to bind objects
3 how will the List object to the Java terminal:
<script type="text/javascript"> $(document).ready(function(){ var saveDataAry=[]; var data1={"userName":"test","address":"gz"}; var data2={"userName":"ququ","address":"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $.ajax({ type:"POST", url:"user/saveUser", dataType:"json", contentType:"application/json", data:JSON.stringify(saveData), success:function(data){ } }); }); </script>
JSON.stringify() : Convert the object to a JSON string.
JSON.parse(): The string JSON is converted to JSON objects.
Java:
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody List<User> users) { userService.batchSave(users); }
This is not possible. Because spring MVC does not automatically translate into a List object. To the background, List is of type LinkedHashMap.
But using the array can receive User[], as follows:
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody User[] users) { userService.batchSave(users); }
- SpringMVC @RequestBody processing Ajax requests
- Ajax+SpringMVC requestBody
- SpringMVC @RequestBody 处理ajax请求
- jquery- $.ajax请求映射springmvc中的@RequestBody
- @RequestBody-------springMVC
- SpringMVC @RequestBody
- 知识库--Processing Requests
- SpringMVC中的 @RequestBody
- springMVC中@requestBody问题
- SpringMVC之@RequestBody, @ResponseBody
- SpringMVC - @Responsebody与@RequestBody
- SpringMVC之@RequestBody详解
- SpringMVC @RequestBody使用
- SpringMVC requestBody responseBody 详解
- springMVC--requestBody和responseBody
- SpringMVC 之RequestBody
- SpringMvc的@RequestBody注解
- SpringMVC @requestBody和requestParam
- NDIS 函 数简明手册
- 【设计模式笔记】单例模式Singleton Pattern
- 回调机制详解
- 大数据:知识,真正的价值体现
- hdu 5013 City Tour
- SpringMVC @RequestBody processing Ajax requests
- Http状态码大全
- (一)storm-kafka源码走读之前言
- POJ 3469 Dual Core CPU(最小割)
- UIButton重绘机制
- 大数据分析,利用向外扩展技术深入挖掘商业价值
- CC1100 CCA(2)
- 第10周 项目4-3 评委打分(终极版)
- tomcat配置虚拟目录