gson 如何 将 Httpclient 的 响应 对象 序列 化 和 反 序列 化 为 字节 数组

blmhpbnm  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(188)

我想序列化和反序列化Apache、okhttp客户端等的http响应对象。
我无法使用-〉

  1. 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)
k4emjkb1

k4emjkb11#

  • 第一个问题是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance oforg.example.Student(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)表明类应该声明一个默认的构造函数,那么默认的构造函数是什么呢,好的,它只是一个无参数的构造函数。
  • Java内存分析,用于分析JVM内存使用情况。

相关问题