无法在Java

时间:2016-01-11 22:32:08

标签: java serialization deserialization

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

MultivaluedHashMap实现java.io.Serializable并且具有public,no arg构造函数。

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

  

线程“main”中的异常org.apache.commons.lang3.SerializationException:java.io.InvalidClassException:javax.ws.rs.core.MultivaluedHashMap;没有有效的构造函数       在org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:231)       在org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:267)       在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)       在java.lang.reflect.Method.invoke(Method.java:497)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   引起:java.io.InvalidClassException:javax.ws.rs.core.MultivaluedHashMap;没有有效的构造函数       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)       在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)       在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)       at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)       在org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223)       ......还有7个

通过运行下面简单的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);
    }
}

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

似乎反序列化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

答案 1 :(得分:0)

这是因为MultivaluedHashMap没有实现Serializable。要解决此问题,请执行以下操作:

import java.io.Serializable;

import javax.ws.rs.core.MultivaluedHashMap;

public class MyMultivaluedHashMap<K, V> extends MultivaluedHashMap<K, V> implements Serializable{
	private static final long	serialVersionUID	= 1L;
	public MyMultivaluedHashMap(){
		super();
		//there are other contruc super class
	}
}

//your code
import javax.ws.rs.core.MultivaluedHashMap;

import org.apache.commons.lang3.SerializationUtils;

public class Teste{
    public Teste(){}
    public static void main(String[] args) throws Exception{
        MyMultivaluedHashMap<String, String> map = new MyMultivaluedHashMap<String, String>();
        map.addFirst("key-1", "chubby");
        map.add("key-2", "skinny");
        byte[] serializedMap = SerializationUtils.serialize(map);
        System.out.println(serializedMap);
        MultivaluedHashMap<String, String> deserializedMap = SerializationUtils.deserialize(serializedMap);
        System.out.println(deserializedMap);
    }
}

因此,您只需创建一个实现Serializable的新类MyMultivaluedHashMap。