解决 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: { ... ...
随机推荐
- hash join
hash join是oracle里面一个非常强悍的功能,当做hash join时,oracle会选择一个表作为驱动表,先根据过滤条件排除不必要的数据,然后将结果集做成hash表,放入进程的hash a ...
- java String中的replace(oldChar,newChar) replace(CharSequence target,CharSequence replacement) replaceAll replaceFirst 面试题:输入英文语句,单词首字符大写后输出 char String int 相互转换
package com.swift; import java.util.Scanner; public class FirstChat_ToCaps_Test { public static void ...
- RabbitMQ学习(二):Java使用RabbitMQ要点知识
转 https://blog.csdn.net/leixiaotao_java/article/details/78924863 1.maven依赖 <dependency> <g ...
- 树莓派编译ncnn
1.从github上下载ncnn git clone --recursive https://github.com/Tencent/ncnn 2.在ncnn根目录下创建build目录,安装cmake编 ...
- 科学计算库Numpy——numpy.ndarray
创建ndarray 元素类型 对于ndarray结构来说,里面所有的元素必须是同一类型的,如果不是的话,会自动的向下进行转换. 元素类型所占字节数 数组维数 元素个数 数组的维度 数组中填充固定值 索 ...
- debian右键添加在终端中打开
sudo apt-get install nautilus-open-terminal -y 注销,重启
- servlet 作用
什么是Servlet Servlet是一个Java编写的程序,此程序是基于Http协议的,在服务器端运行的(如tomcat),是按照Servlet规范编写的一个Java类. 在BS架构中,早期的Web ...
- linux中的部分宏
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- html-body相关标签
一 字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub& ...
- luogu2564 [SCOI2009]生日礼物
排序枚举左端点,则右端点必定不降 #include <algorithm> #include <iostream> #include <cstring> #incl ...