简体   繁体   English

序列化java.lang.class

[英]Serializing java.lang.class

I have a problem persisting the Class object in the DB. 我在将Class对象保留在数据库中时遇到问题。

I tried to convert the object into a byte array using object ObjectArrayOutputStream and ByteArrayOutputStream as shown below and persisted it the byte array: 我尝试使用对象ObjectArrayOutputStreamByteArrayOutputStream将对象转换为字节数组,如下所示,并将其持久保存在字节数组中:

Class klazz = getClassForClassDef();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(klazz);
baos.toByteArray();

But I get an error shown below: 但我收到如下所示的错误:

java.lang.ClassFormatError: Incompatible magic value 2901213189 in class file

I think that the way byte array was constructed has the problem. 我认为字节数组的构造方式存在问题。 But I don't know how to create a correct .class equivalent of the class object. 但是我不知道如何创建一个与类对象等效的.class等效项。

If you are really trying to store the Class itself : 如果您真的想存储Class本身

You can read more about it here: https://stackoverflow.com/a/2390763/1001027 您可以在此处了解更多信息: https : //stackoverflow.com/a/2390763/1001027

If you are trying to store an Object : 如果您要存储对象

You are using the Class as parameter for the writeObject method. 您正在使用Class作为writeObject方法的参数。 Instead of this you should use the instance you have. 代替此,您应该使用您拥有的实例。


// write the class
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(objectToSerialize);

// this you need to store
byte[] data = baos.toByteArray();

// read it again (just a test)
ByteArrayInputStream bais = new ByteArrayInputStream(data);
Object restored = new ObjectInputStream(bais).readObject();

// write what came from the deserialization
System.out.println(restored);

In the sample, the variable objectToSerialize will hold the object to store. 在示例中,变量objectToSerialize将保存要存储的对象。


Are you sure you want really store java serialized objects into your database? 您确定要真正将Java序列化的对象存储到数据库中吗? There are a few alternatives to that. 有一些替代方法。 You could create a formal model using an ORM framework such as Hibernate. 您可以使用ORM框架(例如Hibernate)创建正式模型。

If you think this would be too dynamic, you could use XML serialization such as XStream or even JSON serialization, this would be good because they are readable formats, meaning you can see what is really stored, avoiding the need to deserialize it with a Java application just to check the state. 如果您认为这太动态了,则可以使用XML序列化(例如XStream甚至是JSON序列化),因为这是可读格式,这很好,这意味着您可以查看实际存储的内容,而无需使用Java反序列化它。申请只是为了检查状态。

To serialise an object you have to write into the stream the object itself rather than it's class. 要序列化对象,您必须将对象本身而不是类写入流中。 Additionally, the object instance must implement the Serializable interface and all its internal fields should be either serializable or transient. 此外,对象实例必须实现Serializable接口,并且其所有内部字段都应该是可序列化的或瞬态的。

In your code, instead of oos.writeObject(klazz); 在您的代码中,而不是oos.writeObject(klazz); you should write oos.writeObject(obj); 您应该编写oos.writeObject(obj); , providing obj is the object instance you want to save. ,提供obj是您要保存的对象实例。

However, if you are trying to store a Java object into a DB (we are talking about a relational DB, aren't we?) I would map the object to a table, storing each of its attributes as values for the columns. 但是,如果您试图将Java对象存储到数据库中(我们在谈论关系数据库,不是吗?),我会将对象映射到表,将其每个属性存储为列的值。 That way you would be able to perform searches and joins using SQL and the values for the columns, you can't index binary fields though. 这样,您将能够使用SQL和列的值执行搜索和联接,但是您无法索引二进制字段。

A .class file is not a serialized Class object . .class文件不是序列化的Class对象 It is something completely different, and you will not be able to produce it with any simple Java code. 这是完全不同的东西,您将无法使用任何简单的Java代码来生成它。 However, why would you want to? 但是,为什么要呢? You already have that .class file on disk, that's how your Java program got to know about the class. 您已经在磁盘上拥有该.class文件,这就是您的Java程序如何了解该类的方式。 Why don't you just copy that .class file? 您为什么不只复制该.class文件? It will only be an issue of finding where it comes from (what JAR). 只是找到它的来源(JAR是什么)将是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM