我想序列化和反序列化Apache、okhttp客户端等的http响应对象。
我无法使用-〉
- JavaSerializable作为响应对象的两个客户端都不实现可串行化接口。
1.Jackson因为响应对象没有无参数构造函数,因此在反序列化时显示以下错误。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.example.Student` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
1.Gson由于内存不足错误:
(下面使用ApacheClient)
序列化:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
gson.toJson(httpResponse, writer);
writer.flush();
反序列化:
byte[] data = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
InputStreamReader reader = new InputStreamReader(inputStream);
HttpResponse res = gson.fromJson(reader, HttpResponse.class);
System.out.println("HttpResponse-> " + EntityUtils.toString(res.getEntity()));
错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuffer.append(StringBuffer.java:276)
at java.io.StringWriter.write(StringWriter.java:101)
at com.google.gson.stream.JsonWriter.newline(JsonWriter.java:602)
at com.google.gson.stream.JsonWriter.beforeValue(JsonWriter.java:646)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:532)
at com.google.gson.internal.bind.TypeAdapters$5.write(TypeAdapters.java:189)
at com.google.gson.internal.bind.TypeAdapters$5.write(TypeAdapters.java:173)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ArrayTypeAdapter.write(ArrayTypeAdapter.java:95)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
1条答案
按热度按时间k4emjkb11#
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
org.example.Student(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
表明类应该声明一个默认的构造函数,那么默认的构造函数是什么呢,好的,它只是一个无参数的构造函数。