|
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.itsvse.es.models.CarPosition` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
Cannot construct instance of `com.itsvse.es.models.CarPosition$Point` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor 解决方案:
类添加构造函数,如下:
- public CarPosition(){}
- public Point(){}
复制代码
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('' (code 65279 / 0xfeff)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
错误原因:
我用Java读取的txt文本文件是uft-8 bom编码格式导致的,我将文件文件转换成utf-8会正常!但是由于我文件太多了,总不能每一个都手动转成utf-8格式吧!
引用
EF BB BF 54 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6C 69 6E 65 2E
?This is the first line.
54 68 69 73 20 69 73 20 73 65 63 6F 6E 64 20 6C 69 6E 65 2E
This is second line.
红色部分的"EF BB BF"刚好是UTF-8文件的BOM编码,可以看出Java在读文件时没能正确处理UTF-8文件的BOM编码,将前3个字节当作文本内容来处理了。
解决方案:
maven引用如下包:
- <!-- 处理utf bom问题 -->
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.6</version>
- </dependency>
复制代码 读txt文件代码如下:
- /**
- * 获取txt文件内容并按行放入list中
- */
- public static List<String> getFileContext(String path) throws Exception {
- //FileReader fileReader =new FileReader(path);
- //BufferedReader bufferedReader =new BufferedReader(fileReader);
- BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(path))));
- //reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file))));
- List<String> list =new ArrayList<String>();
- String str=null;
- while((str=bufferedReader.readLine())!=null) {
- if(str.trim().length()>2) {
- list.add(str);
- }
- }
- return list;
- }
复制代码 我测试无论是读取utf-8 bom文件,还是utf-8格式的文件,都能成功转成utf-8格式的文件,并且反序列化成功!
最后说一句,再也不用fastjson!碰到阿里系的开源,如果不是优势天差地别,绕道别碰。
|
上一篇:2018汪文君Google Guava实战视频教程下一篇:Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题
|