解决 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)
首先检查是否是 objectMapper.enableDefaultTyping(); 的受害者。优先考虑删除该配置。
使用Jackson把数组的json字符串反序列化为List时候报了个JsonMappingException。
java.lang.UnsupportedOperationException: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.util.List)
at [Source: [ ......
找到问题代码,粗看似乎没什么问题?
List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<List<MyObject>>() {}); //jsonString是个json对象的数组
注意到异常信息“need JSON String that contains type id (for subtype of java.util.List)”。想了一会儿,好吧,有答案了。
List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<ArrayList<MyObject>>() {}); //jsonString是个json对象的数组
其实,有一种比较老派的反序列化为List的方式...
List<MyObject> objectList = Arrays.asList(objectMapper.readValue(jsonString, MyObject[].class)); //jsonString是个json对象的数组
当对一些较复杂的对象进行反序列化时,例如拥有接口类型成员变量的类。举个栗子:
@Data
public class TypeValue {
private Integer type;
private List<Integer> value;
}
有上面这个类,需要把json字符串反序列化成 Map<String, TypeValue> 这样的对象,怎么做?
可以在TypeValue这个类中使用 @JsonCreator 注解解决。
@Data
public class TypeValue {
private Integer type;
private List<Integer> value; @JsonCreator //为Jackson提供构造函数
public TypeValue(@JsonProperty("type") final Integer type, @JsonProperty("value") final int[] value) {
this.type= type;
this.value = Ints.asList(value);
}
}
Jackson能够把[]数组转换为List。因此可以用以上方法绕过Jackson的限制。
以上使用了guava和lombok
解决 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)的更多相关文章
- Unexpected token '...'. Expected a property name.
原因:浏览器不支持 es6 扩展运算符 结论: 1.不用 ... 2. babel-polyfill对扩展运算符...搞的不是太好,要单独安装一个 plugin-proposal-object-re ...
- Appium+python自动化54-appium-doctor报错已解决(SyntaxError: Unexpected token ...)
前言 由于新版的appium desktop版本是不带appium-doctor这个包的,所以想用appium-desktop检查环境的话需要另外的安装了,在安装的时候小编又遇到了一个坑 报错信息:S ...
- [bug] Unrecognized token 'code': was expecting (JSON String, Number, Array, Object,'true', 'false' or 'null')
JSON格式有误,需用JSON.stringify()函数转换一下 参考 https://www.cnblogs.com/sunyanblog/p/13788740.html https://www. ...
- 启动webpack-dev-server错误,ERROR in main.js from UglifyJs Unexpected token: name «element», expected: punc «;»
启动webpack-dev-server出现以下错误 ERROR in main.js from UglifyJsUnexpected token: name «element», expected: ...
- vuex2中使用mapMutations/mapActions报错解决方法 BabelLoaderError: SyntaxError: Unexpected token
在尝鲜vuex2时,发现vuex2增加了 mapGetters 和 mapActions 的方法,借助stage2的 Object Rest Operator 特性,可以写出下面代码:methods: ...
- ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决
ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决 根据异常提示:我找了我的MySQL语句:果然 ...
- Uncaught SyntaxError: Unexpected token <解决方法
最近剥离基础框架的公共部分,早上有个页面部分流程未加载出来,报了Uncaught SyntaxError: Unexpected token <,网上搜了下 错误原因:js脚本中非正常引用外部的 ...
- 解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题
参考:https://blog.csdn.net/u012453843/article/details/69803244 解决执行脚本报syntax error: unexpected end of ...
- 解决 vuex mapGetters 语法报错 (Unexpected token )
在使用vuex2的mapGetters 和 mapActions 的方法时,借助 stage2 的 Object Rest Operator 特性,可以写出下面代码: computed: { ... ...
随机推荐
- linux readahead
blockdev --getra /dev/sda blockdev --setra 2048 /dev/sda 必须将其写入配置文件/etc/rc.local,否则重启就会失效.[root@loca ...
- 获取kafka的lag, offset, logsize的shell和python脚本
python脚本 #!/usr/bin/env python import os import re import sys group_id=sys.argv[1] pn=sys.argv[2] ka ...
- Mybatis中关于OGNL表达式冲突
注意设计表字段不能用bor xor and band eq neq lt gt lte gte shl shr ushr
- linux配置邮件客户端
linux配置邮件客户端 1. 申请一个163邮箱,并配置客户端授权密码 l 开启POP3/SMTP/IMAP l 设置客户端授权密码 ###此密码不能跟邮箱密码相同,此密码用来在linux邮件客户端 ...
- c++ 拷贝资源方法
#include "stdio.h" #include "stdlib.h" #include <sys/types.h> #include < ...
- JavaScript中面向对象的三大特性(一个菜鸟的不正经日常)
经过几天的学习,把jQuery给啃会了,但是运用的还不算特别熟练,总感觉自己在JavaScript方面的基础十分欠缺,所以继续拾起JavaScript,开始更好的编程之旅~ 今天学的是JavaScri ...
- Linux基础学习-网络管理
Linux系统网络管理NetworkManager 1 启动网络管理服务和开机自启动 在rhel7中网路管理相关命令nmcli,nmtui,nmtui-edit,nm-connection-edito ...
- SpringMVC 项目中引用其他 Module 中的方法
1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)
- python向上取整 向下取整
向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...
- jsp内置对象及其方法
JSP中一共预先定义了9个这样的对象,分别为: request. response. session. application. out. pagecontext. con ...