gpt4 book ai didi

java - 在 Memcache 上工作时出现不可序列化的对象错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:29 25 4
gpt4 key购买 nike

大家好,我正在使用内存缓存,当我编译这段代码时出现以下错误。

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

我设法通过将 User 类转换为 String 来修复它,但我不想使用 toString 方法。我只想使用我的类来添加和获取方法。我该如何解决这个问题?这是我正在使用的代码。

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();
}
}
}

对不起,我忘记了 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();
}


}

最佳答案

内存缓存不知道如何序列化您的对象。您需要实现 Serializable 以使 Java 处理序列化,或者如果您需要对(反)序列化过程进行更多控制,则需要实现 Externalizable

关于java - 在 Memcache 上工作时出现不可序列化的对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3210702/

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