java 处理 Memcache 时出现不可序列化的对象错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3210702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-serializable object error while working on Memcache
提问by mehmetozer
Hi everyone i was working with memcache, and when i compiled this code i got following errors.
大家好,我正在使用 memcache,当我编译此代码时,出现以下错误。
2010-07-09 10:35:53.499 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2010-07-09 10:35:53.520 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@7fdcde
Exception in thread "main" java.lang.IllegalArgumentException: Non-serializable object
at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:86)
at net.spy.memcached.transcoders.SerializingTranscoder.encode(SerializingTranscoder.java:135)
at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:301)
at net.spy.memcached.MemcachedClient.set(MemcachedClient.java:691)
at def.Tweet.main(Tweet.java:23)
Caused by: java.io.NotSerializableException: def.User
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1173)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:343)
at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:81)
... 4 more
I managed to fix it with converting User class toString but ? dont want to use toString method.I just want to use my class to add and get mwethods. How can i fix this problem? Here is the code i am using.
我设法通过将 User 类转换为 String 来修复它,但是?不想使用 toString 方法。我只想使用我的类来添加和获取 mwethods。我该如何解决这个问题?这是我正在使用的代码。
package def;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import net.spy.memcached.MemcachedClient;
public class Tweet {
private static User user;
private static Message message;
private static Followers follower;
public static void main(String[] args) throws Exception {
try{
user = new User(1,"Mehmet ?zer","asd","127.0.0.1","True","Onine");
//String deneme = user.toString();
MemcachedClient c=new MemcachedClient(new InetSocketAddress("localhost", 11211));
// Store a value (async) for one hour
c.set(user.userKey, 3600, user);
System.out.println(c.get(user.userKey));
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
Sorry , i forgot the User class, I am also giving you the User class to help me better.
对不起,我忘记了 User 类,我也给你 User 类来帮助我更好地帮助我。
public class User {
public static String userKey = "UserDB";
public static int userID ;
public static String userName;
public static String password;
public static String ipAddress;
public static String isOnline;
public static String lastActive;
public User(int userID, String userName, String password, String ipAddress,String isOnline,String lastActive)
{
this.userID = userID;
this.userName = userName;
this.password = password;
this.ipAddress = ipAddress;
this.isOnline = isOnline;
this.lastActive = lastActive;
}
@Override
public String toString() {
System.out.println(this.userID);
System.out.println(this.userName);
System.out.println(this.password);
System.out.println(this.ipAddress);
System.out.println(this.isOnline);
System.out.println(this.lastActive);
return super.toString();
}
}
回答by Niels van der Rest
Memcache doesn't know how to serialize your objects. You'll need to implement Serializableto have Java handle the serialization, or Externalizableif you need more control over the (de)serialization process.
Memcache 不知道如何序列化您的对象。您需要实现Serializable让 Java 处理序列化,或者Externalizable如果您需要对(反)序列化过程进行更多控制。
回答by Riduidel
Updated due to question modification.
由于问题修改而更新。
So, your User class must implements the Serializable. Hopefully, this is as simple as writing
因此,您的 User 类必须实现Serializable。希望这就像写作一样简单
public class User implements Serializable {
...
}
as Serializable does not contains any method.
因为 Serializable 不包含任何方法。
回答by Rajeev Rathor
This problem is coming because, JVM is not able to serialized object corrctly because of conflict in schema. In my case, Serial version ID of some classes are defined 1L as default. These classes are corporately to each others. I assigned some unique custom value. My Problem resolved.
出现这个问题的原因是,由于模式冲突,JVM 无法正确序列化对象。就我而言,某些类的串行版本 ID 被定义为 1L 作为默认值。这些类是相互关联的。我分配了一些独特的自定义值。我的问题解决了。

