0x00 前言
在工作开发中,有一个需求需要将项目中的fastjson迁移至gson,在迁移过程中发现了一个奇葩的bug,如下:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: (was java.lang.IllegalStateException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.IllegalStateException) (through reference chain: com.demo.boot.web.common.RestWebResultContainer["data"]->com.google.gson.JsonArray["asInt"])
0x01 问题分析
看到第一眼知道是反序列化失败,但是用fastjson的JSONArray就没问题,为啥换成gson的的JsonArray就有这个问题了呢,后续通过对于两者的源码分析看出。
fastjson中的JSONArray
package com.alibaba.fastjson;
import com.alibaba.fastjson.JSONObject.SecureObjectInputStream;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import java.io.IOException;
import java.io.NotActiveException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.RandomAccess;
public class JSONArray extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable {
private static final long serialVersionUID = 1L;
private final List<Object> list;
protected transient Object relatedArray;
protected transient Type componentType;
.......
public Object get(int index) {
return this.list.get(index);
}
public JSONObject getJSONObject(int index) {
Object value = this.list.get(index);
if (value instanceof JSONObject) {
return (JSONObject)value;
} else {
return value instanceof Map ? new JSONObject((Map)value) : (JSONObject)toJSON(value);
}
}
public JSONArray getJSONArray(int index) {
Object value = this.list.get(index);
if (value instanceof JSONArray) {
return (JSONArray)value;
} else {
return value instanceof List ? new JSONArray((List)value) : (JSONArray)toJSON(value);
}
}
public <T> T getObject(int index, Class<T> clazz) {
Object obj = this.list.get(index);
return TypeUtils.castToJavaBean(obj, clazz);
}
public <T> T getObject(int index, Type type) {
Object obj = this.list.get(index);
if (type instanceof Class) {
return TypeUtils.castToJavaBean(obj, (Class)type);
} else {
String json = JSON.toJSONString(obj);
return JSON.parseObject(json, type, new Feature[0]);
}
}
public Boolean getBoolean(int index) {
Object value = this.get(index);
return value == null ? null : TypeUtils.castToBoolean(value);
}
public boolean getBooleanValue(int index) {
Object value = this.get(index);
return value == null ? false : TypeUtils.castToBoolean(value);
}
public Byte getByte(int index) {
Object value = this.get(index);
return TypeUtils.castToByte(value);
}
public byte getByteValue(int index) {
Object value = this.get(index);
Byte byteVal = TypeUtils.castToByte(value);
return byteVal == null ? 0 : byteVal;
}
public Short getShort(int index) {
Object value = this.get(index);
return TypeUtils.castToShort(value);
}
public short getShortValue(int index) {
Object value = this.get(index);
Short shortVal = TypeUtils.castToShort(value);
return shortVal == null ? 0 : shortVal;
}
public Integer getInteger(int index) {
Object value = this.get(index);
return TypeUtils.castToInt(value);
}
public int getIntValue(int index) {
Object value = this.get(index);
Integer intVal = TypeUtils.castToInt(value);
return intVal == null ? 0 : intVal;
}
public Long getLong(int index) {
Object value = this.get(index);
return TypeUtils.castToLong(value);
}
public long getLongValue(int index) {
Object value = this.get(index);
Long longVal = TypeUtils.castToLong(value);
return longVal == null ? 0L : longVal;
}
public Float getFloat(int index) {
Object value = this.get(index);
return TypeUtils.castToFloat(value);
}
public float getFloatValue(int index) {
Object value = this.get(index);
Float floatValue = TypeUtils.castToFloat(value);
return floatValue == null ? 0.0F : floatValue;
}
public Double getDouble(int index) {
Object value = this.get(index);
return TypeUtils.castToDouble(value);
}
public double getDoubleValue(int index) {
Object value = this.get(index);
Double doubleValue = TypeUtils.castToDouble(value);
return doubleValue == null ? 0.0D : doubleValue;
}
public BigDecimal getBigDecimal(int index) {
Object value = this.get(index);
return TypeUtils.castToBigDecimal(value);
}
public BigInteger getBigInteger(int index) {
Object value = this.get(index);
return TypeUtils.castToBigInteger(value);
}
public String getString(int index) {
Object value = this.get(index);
return TypeUtils.castToString(value);
}
public Date getDate(int index) {
Object value = this.get(index);
return TypeUtils.castToDate(value);
}
public java.sql.Date getSqlDate(int index) {
Object value = this.get(index);
return TypeUtils.castToSqlDate(value);
}
public Timestamp getTimestamp(int index) {
Object value = this.get(index);
return TypeUtils.castToTimestamp(value);
}
.........
}
gson中的JsonArray
package com.google.gson;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public final class JsonArray extends JsonElement implements Iterable<JsonElement> {
private final List<JsonElement> elements;
......
public Number getAsNumber() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsNumber();
} else {
throw new IllegalStateException();
}
}
public String getAsString() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsString();
} else {
throw new IllegalStateException();
}
}
public double getAsDouble() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsDouble();
} else {
throw new IllegalStateException();
}
}
public BigDecimal getAsBigDecimal() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsBigDecimal();
} else {
throw new IllegalStateException();
}
}
public BigInteger getAsBigInteger() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsBigInteger();
} else {
throw new IllegalStateException();
}
}
public float getAsFloat() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsFloat();
} else {
throw new IllegalStateException();
}
}
public long getAsLong() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsLong();
} else {
throw new IllegalStateException();
}
}
public int getAsInt() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsInt();
} else {
throw new IllegalStateException();
}
}
public byte getAsByte() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsByte();
} else {
throw new IllegalStateException();
}
}
public char getAsCharacter() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsCharacter();
} else {
throw new IllegalStateException();
}
}
public short getAsShort() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsShort();
} else {
throw new IllegalStateException();
}
}
public boolean getAsBoolean() {
if (this.elements.size() == 1) {
return ((JsonElement)this.elements.get(0)).getAsBoolean();
} else {
throw new IllegalStateException();
}
}
........
}
通过对比可以看出JsonArray中有大量的get方法且无参,这样会导致将JsonArray作为序列化对象传输后在对其进行反序列化后会将例如getAsInt转化成asInt属性,这样找不到会报错,上述错误是找不到asInt的set方法,所以出现类似错误时可以看看对应类型的getter/setter方法有没有写,并且类型是否对应。
0x02 问题解决
找到错误原因后果断放弃将JsonArray作为回传对象,采用List回传 ,接口数据返回正常。
JsonArray jsonArray = xxx;
return Arrays.asList(new Gson().fromJson(jsonArray, Dto[].class));
0x03 总结
有问题不要乱尝试,要学会分析,不要猜,和女人的心思一样,特别难猜到!!!!
经过此次折磨的BUG , 个人觉得fastjson更合适作为一个对象,而gson更像是一个工具类。