开发者社区> 六月的雨在钉钉> 正文

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `object` out of START_ARRAY token

简介: 讲述如何处理 org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `object` out of START_ARRAY token的问题
+关注继续查看

问题背景

详细的问题背景接上一篇博文Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 下面我们来解决上一篇博文中处理好ajax content-type报错问题之后的另一个问题

报错信息

报错信息内容如下

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.dongao.project.tmupgradeorderrecord.domain.TmUpgradeOrderRecord` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.dongao.project.tmupgradeorderrecord.domain.TmUpgradeOrderRecord` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1]

这个报错信息的意思翻译过来也就是说:无法从START_ARAY令牌中反序列化“后端controller接收参数对象”的实例,简单理解就是前端传json参数到后端,在映射到对象字段的时候报错,那么这个问题怎么处理呢?

问题处理

首先我们需要知道前端传参的json格式是否是正确的格式,前端数据内容我们通过在页面添加console.log(data);打印在控制台

[{"name":"phone","value":"3"},{"name":"gwUserName","value":"3"},{"name":"orderNo","value":"1"},{"name":"deductionGoodsIds","value":"1"},{"name":"orderNo","value":"2"},{"name":"deductionGoodsIds","value":"2"},{"name":"gwlist","value":[{"orderNo":"1"},{"orderNo":"2"},{"orderNo":""}]}]

然后我们把入参放入到json格式化工具中可以看到前端数据可以正常转为json结构,说明数据没有问题

image.png

那么前端数据没有问题,但是后端报错反序列化问题,那么问题出在哪儿呢?这里我们再来看一下ajax 传参json格式数据的展示

image.png

在对比我们往后端传的参数可以观察到前后端多了“[”“]”,下面我们需要改造$('#form-tmUpgradeOrderRecord-add').serializeArray();方法

改造

这里我们改造后的代码

// 获取form下所有的字段并转换为json对象
formToJSON: function(formId) {
    var json = {};
    $.each($("#" + formId).serializeArray(), function(i, field) {
        json[field.name] = field.value;
    });
    return json;
}

改造页面代码

image.png

页面json入参控制台打印

{"phone":"3","gwUserName":"3","orderNo":"2","deductionGoodsIds":"2","gwlist":[{"orderNo":"1"},{"orderNo":"2"},{"orderNo":""}]}

此时的json入参已经没有前后的“[ ]”符号了,后端可以正常解析json入参到对象字段。

后端controller debug可以看到接收入参的对象中已经可以成功收到子表list数据了

image.png

至此,整个这关于前端json传参,后端解析json参数反序列化到对象的操作就算完成了,大功告成,后面就可以开心丰富业务逻辑内容了。


版权声明:本文内容由便宜云服务器实名注册用户自发贡献,版权归原作者所有,便宜云服务器开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《便宜云服务器开发者社区用户服务协议》和《便宜云服务器开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
JAVA解决方案:Missing URI template variable ‘customersno‘ for method parameter of type String
解决方案 PathVariable和RequestParam的使用 @PathVariable @RequestParam PathVariable和RequestParam的区别 区别在于一个是用?,一个使用/进行传输数据
78 0
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
958 0
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
62 0
调用Feign接口报错:JSON parse error:Illegal character ((CTRL-CHAR, code 31))
调用Feign接口报错:JSON parse error:Illegal character ((CTRL-CHAR, code 31))
2261 0
org.activiti.engine.ActivitiException: Couldn't deserialize object in variable 'application'
org.activiti.engine.ActivitiException: Couldn't deserialize object in variable 'application'
268 0
HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingEx
HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingEx
340 0
Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
1115 0
JSON.parse()报错 Unexpected token o in JSON at position 1
JSON.parse()报错 Unexpected token o in JSON at position 1
111 0
Collecting package metadata (current_repodata.json): failed
Collecting package metadata (current_repodata.json): failed
318 0
解决:Could not write JSON: No serializer found for class *** and no properties错误
解决:Could not write JSON: No serializer found for class *** and no properties错误
365 0
+关注
六月的雨在钉钉
从事java行业8年至今,热爱技术,热爱以博文记录日常工作,csdn博主,工作座右铭是:让技术不再枯燥,让每一位技术人爱上技术
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
Apache Wicket User Guide - Ref
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载


http://www.vxiaotou.com