gpt4 book ai didi

java - 无法在 Java 中反序列化 MultivaluedHashMap

转载 作者:行者123 更新时间:2023-11-30 10:51:26 24 4
gpt4 key购买 nike

我正在尝试反序列化 javax.ws.rs.core.MultivaluedHashMap。

MultivaluedHashMap 实现了 java.io.Serializable 并且有一个公共(public)的、无参数的构造函数。

但是,当反序列化之前序列化的 MultivaluedHashMap 时,抛出 InvalidClassException,没有有效的构造函数:

Exception in thread "main" org.apache.commons.lang3.SerializationException: java.io.InvalidClassException: javax.ws.rs.core.MultivaluedHashMap; no valid constructor at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:231) at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:267) at org.arx.serializationtest.Main.main(Main.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.io.InvalidClassException: javax.ws.rs.core.MultivaluedHashMap; no valid constructor at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150) at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:768) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1775) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223) ... 7 more

这很容易通过运行下面简单的 3 行长的主要方法重现:

package test;

import org.apache.commons.lang3.SerializationUtils;
import javax.ws.rs.core.MultivaluedHashMap;

public class Test {
public static void main(String[] args) throws Exception {

MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
byte[] serializedMap = SerializationUtils.serialize(map);

MultivaluedHashMap<String, String> deserializedMap = SerializationUtils.deserialize(serializedMap);
}
}

从那里我不确定如何解决这个问题。任何建议或指示将不胜感激。

预先感谢您的帮助。

最佳答案

反序列化 MultivaluedHashMap 所需的类之一似乎没有无参数构造函数。你可以这样做:

MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
HashMap<String, List<String>> serMap = new HashMap<String, List<String>>(map);
byte[] serializedMap = SerializationUtils.serialize(serMap);

HashMap<String, List<String>> tempMap = SerializationUtils.deserialize(serializedMap);
MultivaluedHashMap<String, String> deserializedMap = new MultivaluedHashMap<String, String>();
for (Entry<String, List<String>> entry : tempMap.entrySet()) {
deserializedMap.put(entry.getKey(), entry.getValue());
}

这样做是为了创建一个 HashMap,它将保存重建原始 MultivaluedHashMap 和序列化/反序列化此 HashMap 所需的所有值而不是 MultivaluedHashMap

关于java - 无法在 Java 中反序列化 MultivaluedHashMap<String, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34732363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com