JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth
java8新出的YearMonth可以方便的用来表示某个月。我的项目中使用springmvc来接收YearMonth类型的数据时发现 x-www-from-urlencoded 格式的数据可以使用"2018-12"的类型接收,但是在post请求中 接收application/json的数据时出现以下错误
2020-02-18 11:18:25.284 WARN 16212 --- [nio-8090-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth;
通过异常应该是YearMonth没有公有构造函数 ,fastjson不支持解析YearMonth;案例使用fastjson作为了springmvc的序列化工具,类似如下的配置
@Configuration
public class MyConverter implements WebMvcConfigurer { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
converters.add(0,fastJsonHttpMessageConverter);
}
}
通过查看MonthYear确实发现其私有化了构造函数,只留了两静态方法
private YearMonth(int year, int month) {
this.year = year;
this.month = month;
}
public static YearMonth of(int year, int month) {
YEAR.checkValidValue(year);
MONTH_OF_YEAR.checkValidValue(month);
return new YearMonth(year, month);
}
private YearMonth with(int newYear, int newMonth) {
if (year == newYear && month == newMonth) {
return this;
}
return new YearMonth(newYear, newMonth);
}
不过get请求时能获取得到,说明jackson应该是对其有专门的处理工具的。将springmvc的序列化工具修改为默认的jackson,发现能 将application/json请求中 “2018-12”格式的数据 顺利的反序列化YearMonth类型。然后通过查找发现jackson确实带有YearMonth的序列化反序列化工具
(看来springmvc将jackson作为默认序列化工具还是有原因的。。)
所以解决方法看来也找到了。就是使用默认的jackson作为序列化工具就可以解决了。
当然了,如果项目不好更改,想使用fastjson也有解决办法。fastjson允许为字段定制反序列化工具,然后对应字段标明使用指定的反序列化类即可 (序列化原理类似)
public class YearMonthDeserializer implements ObjectDeserializer {
@Override
public YearMonth deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.getLexer();
String s = lexer.stringVal();
lexer.nextToken(16);
return YearMonth.parse(s);
} @Override
public int getFastMatchToken() {
return 0;
}
}
在需要反序列化的时候对需要的字段加上标注即可
public class AddVO { @JSONField(deserializeUsing = YearMonthDeserializer.class)
private YearMonth yearMonth; public YearMonth getYearMonth() {
return yearMonth;
} public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
}
当然也可以全局处理,在我们配置fastjson作为httpMessageConverter的地方代码改为如下即可
@Configuration
public class MyConverter implements WebMvcConfigurer { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = fastJsonHttpMessageConverter.getFastJsonConfig();
fastJsonConfig.getParserConfig().putDeserializer( YearMonth.class,new YearMonthDeserializer());
converters.add(0,fastJsonHttpMessageConverter); }
}
JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth的更多相关文章
- com.alibaba.fastjson.JSONException: default constructor not found. class ……
1.json工具类 package com.hyzn.fw.util; import java.util.List; import java.util.Map; import com.alibaba. ...
- JSON parse error: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.58; nested exception is com.alibaba.fastjson.JSONExcetion: syntax error, except {, actual error, pos ...
这个报错信息告诉你,你提交的参数需要是json类型.所以,POST请求携带的数据需要序列化一下json.dumps(data).
- JSON parse error: No suitable constructor found for type
错误信息: 2019-02-19 09:17:58,678 [http-nio-8080-exec-1] WARN org.springframework.web.servlet.mvc.suppor ...
- 【开发遇到的问题】Spring Mvc使用Jackson进行json转对象时,遇到的字符串转日期的异常处理(JSON parse error: Can not deserialize value of type java.util.Date from String[)
1.问题排查 - 项目配置 springboot 2.1 maven配置jackson - 出现的场景: 服务端通过springmvc写了一个对外的接口,查询数据中的表,表中有一个字段属性是时间戳,返 ...
- JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc
代码程序: @PostMapping("selectById") @ResponseBody public Result selectById(@RequestBody int i ...
- JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String
在使用Postman测试Spring Boot项目接口时,接口返回JSON parse error: Cannot deserialize value of type `java.time.Local ...
- JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of j
异常信息如下: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token ...
- java开发客户端发送请求到服务器端出现这样:JSON parse error: Unexpected character ('}' (code 125)): was expecting
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected cha ...
- [待解决]报错:JSON parse error: Unexpected character
{"code":"9999","message":"JSON parse error: Unexpected character ...
随机推荐
- Oracle VM VirtualBox - ping不通虚拟机
问题描述 用Oracle VM VirtualBox创建虚拟机后,本机电脑ping不通虚拟机 解决方案 https://www.cnblogs.com/ranrongzhen/p/6958485.ht ...
- 基于Java+HttpClient+TestNG的接口自动化测试框架(八)------ 针对文件的处理
在实际的接口测试中,有时需要根据情况进行文件的上传和下载.在文件数量比较小的时候,我们当然可以直接处理(比如若干个接口都用一个文件).但是,如果我们上传的文件需要使用不同文件夹里不同的文件,而且数量又 ...
- python 百万级别类实例实现节省内存
# 案例: ''' 某网络游戏中,定义了玩家类Player(id,name,status) 每当有一个玩家,就会在服务器创建一个Player实例 当在线人数过多时,将产生大量实例(百万级别),消耗内存 ...
- mediasoup-demo解析-服务端
1.启动server npm start启动服务,会执行脚本: "start": "DEBUG=${DEBUG:='*mediasoup* *INFO* *WARN* * ...
- sql语句代码规范
19年年底的时候领导一直强调代码规范化以前写代码的时候很随意后来越来越看自己写的代码难受逐渐的也像规范化走去,今天又学了一招记录分享一下 这张图就是以前写代码的时候正常情况很是杂乱无章 这张就是规范话 ...
- form介绍
form组件的主要功能: 1.生成可用的html标签 2.对用户提交的数据进行效验 3.保留上次输入的内容 1.以普通方式手写注册功能 1.渲染前端标签获取用户输入 >>>>渲 ...
- 第四十四篇 入门机器学习——matplotlib基础——实现数据可视化
No.1. 绘制一条正弦曲线 No.2. 在一张图中绘制多条曲线 No.3. 可以为曲线指定颜色.线条样式 No.4. 可以指定横纵坐标轴的范围 也可以使用: No.6. 可以为每条曲线添加图示 No ...
- 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
#include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- jQuery对象和语法
jQuery类型 引入jquery.js时,其实是向全局作用域中,添加了一个新的类型--jQuery. 构造函数:负责创建jQuery类型的对象. 原型对象:保存jQuery对象可用的所有简化版API ...