-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[BUG]2.0.41版本,使用的json字符串中的Integer parseObject为Long时报java.lang.Integer cannot be cast to java.lang.Long #1942
Comments
麻烦提供下完整的测试用例 |
SysDic.java
JSONTest.java public class JSONTest {
@Test
public void idRevert(){
String json = "{\"id\":1,\"name\":\"joe\"}";
JSONObject obj = JSON.parseObject(json);
SerializeConfig config = new SerializeConfig();
config.put(Integer.class, DecimalSerializer.instance);
// ParserConfig parserConfig = new ParserConfig();
// parserConfig.putDeserializer(Integer.class, DecimalSerializer.instance);
SysDic dic = JSON.parseObject(json, SysDic.class);
dic = obj.toJavaObject(SysDic.class);
System.out.println();
}
} 异常结果如帖; |
再三测试,会随机(一半以上几率)出现,回退至2.0.40版本后问题消失; |
DecimalSerializer的代码提供下 |
public class DecimalSerializer implements ObjectSerializer {
public final static SwaggerJsonSerializer instance = new SwaggerJsonSerializer();
private static final DecimalFormat format = new DecimalFormat("#.################");
public void write(JSONSerializer serializer,
Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.getWriter();
String value;
if(object instanceof Long || object instanceof Float) {
value = format.format(object);
}
else {
value = object.toString();
}
out.write(value);
}
} |
问题无法重现 |
我再多研究下 |
已经找到原因:系泛型问题,对应的id字段是作为泛型在基类(接口)中进行的定义(getter、setter)方法,实现类中使用了Long进行了继承,并创建了对应Long型id字段,但未显示的创建getter和setter方法,而是使用lombok注解@DaTa来生成的,这种情况下在fastjson的2.0.40之后的版本会出现,在实现层显示创建getter和setter方法即可。 |
@wenshao 建议针对上述描述,可以检查下是否有这个问题,如果确系lombok或泛型的问题,我就关闭问题,但在2.0.40版本确是好的。 |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
问题描述
web前端修改实体时,id为long型,提交后报java.lang.Integer cannot be cast to java.lang.Long错误
环境信息
请填写以下信息:
重现步骤
示例代码段:
String json = "{"id":1,"name":"joe"}";
JSONObject obj = JSON.parseObject(json);
SerializeConfig config = new SerializeConfig();
config.put(Integer.class, DecimalSerializer.instance);
// ParserConfig parserConfig = new ParserConfig();
// parserConfig.putDeserializer(Integer.class, DecimalSerializer.instance);
SysDic dic = JSON.parseObject(json, SysDic.class);
dic = obj.toJavaObject(SysDic.class);
System.out.println();
输出日志:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Disconnected from the target VM, address: '127.0.0.1:51458', transport: 'socket'
Process finished with exit code -1
The text was updated successfully, but these errors were encountered: