首先检查是否是 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 ...)的更多相关文章

  1. Unexpected token '...'. Expected a property name.

    原因:浏览器不支持 es6 扩展运算符  结论: 1.不用 ... 2. babel-polyfill对扩展运算符...搞的不是太好,要单独安装一个 plugin-proposal-object-re ...

  2. Appium+python自动化54-appium-doctor报错已解决(SyntaxError: Unexpected token ...)

    前言 由于新版的appium desktop版本是不带appium-doctor这个包的,所以想用appium-desktop检查环境的话需要另外的安装了,在安装的时候小编又遇到了一个坑 报错信息:S ...

  3. [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. ...

  4. 启动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: ...

  5. vuex2中使用mapMutations/mapActions报错解决方法 BabelLoaderError: SyntaxError: Unexpected token

    在尝鲜vuex2时,发现vuex2增加了 mapGetters 和 mapActions 的方法,借助stage2的 Object Rest Operator 特性,可以写出下面代码:methods: ...

  6. ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决

    ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决 根据异常提示:我找了我的MySQL语句:果然 ...

  7. Uncaught SyntaxError: Unexpected token <解决方法

    最近剥离基础框架的公共部分,早上有个页面部分流程未加载出来,报了Uncaught SyntaxError: Unexpected token <,网上搜了下 错误原因:js脚本中非正常引用外部的 ...

  8. 解决执行脚本报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 ...

  9. 解决 vuex mapGetters 语法报错 (Unexpected token )

    在使用vuex2的mapGetters 和 mapActions 的方法时,借助 stage2 的 Object Rest Operator 特性,可以写出下面代码:  computed: { ... ...

随机推荐

  1. Java(面试题):字符串截取

    在Java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符. 但对应的字节数不同,一个汉字占两个字节. 定义一个方法,按照指定的字节数来取子串. 如:对于“ab你好”,如果取三个字 ...

  2. 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址

    在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...

  3. iOS开发遇到的坑之六--使用cocopods管理第三方库时,编译出现Library not found for -lPods问题的解决办法

    在项目中有时候会遇到Library not found for -lPods(这里的IPods指的是你具体的第三方库)的问题 出现这个错误的原因是:xcode在编译的时候找不到这个库,从而导致项目无法 ...

  4. HTML与XHTML区别

    1. html超文本标记语言,xhtml可扩展超文本标记语言,xhtml是将html作为xml的应用重新定义的一个标准. 2. xhtm比html的代码规则严格很多,例如'a < b'在xhtm ...

  5. JS数据结构与算法--单向链表

    链表结构:链表中每个元素由一个存储元素本身的节点和一个指向下一元素的引用组成.如下所示(手画的,比较丑,懒得用工具画了,嘻嘻) 1.append方法,向链表末尾插入一个节点 2.insert(posi ...

  6. Laravel核心解读--Console内核

    Console内核 上一篇文章我们介绍了Laravel的HTTP内核,详细概述了网络请求从进入应用到应用处理完请求返回HTTP响应整个生命周期中HTTP内核是如何调动Laravel各个核心组件来完成任 ...

  7. angular4使用代理

    1. 在angular-cli项目根目录下创建proxy.config.json { "/api/v1": { "target": "http://1 ...

  8. Golang map并发 读写锁

    golang并发 一:只有写操作 var ( count int l = sync.Mutex{} m = make(map[int]int) ) //全局变量并发写 导致计数错误 func vari ...

  9. matplotlib学习记录 三

    # 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...

  10. Party Games UVA - 1610 贪心

    题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...