gpt4 book ai didi

map - Hazelcast IMap.get() 抛出 HazelcastSerializationException,但 IMap.put() 工作正常

转载 作者:行者123 更新时间:2023-12-03 23:10:28 34 4
gpt4 key购买 nike

我有一个在 VM 上运行的 Hazelcast 服务器实例。数据应该存储在 MAP<Integer, User> ,其中User类如下:

public class User implements com.hazelcast.nio.serialization.DataSerializable{
private Integer id;
private String name;
private String nick;
private Boolean sex;

//getters & setters

@Override
public void writeData(ObjectDataOutput out) throws IOException{
out.writeInt(id.intValue());
out.writeUTF(name);
out.writeUTF(nick);
out.writeBoolean(sex);
}

@Override
public void readData(ObjectDataInput in) throws IOException{
id = (Integer) in.readInt();
name = in.readUTF();
nick = in.readUTF();
sex = in.readBoolean();
}

我使用客户端实例连接到此服务器并尝试将对象添加到该 map :

    System.out.println("Map Size: " + map.size());
map.put(1, user);
System.out.println("Map Size: " + map.size());

System.out.println(map.containsKey(1) ? "yes":"no");
System.out.println(map.containsValue(user) ? "yes":"no");
User queried = (User) map.get(1); /*this is line 64*/

System.out.println(queried.toString());

上面的代码给了我以下控制台输出:

    Map Size: 0
Map Size: 1
yes
yes

还有以下 User queried = (User) map.get(1); 行的异常:

Problem while reading DataSerializable, namespace: 0, id: 0, class: com.blabla.User, exception: com.blabla.User.<init>()

这里有什么问题?为什么我无法读取刚刚放入 Map 的数据?

以下是异常详情:

com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:114)
com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:36)
com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:59)
com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:218)
com.hazelcast.client.spi.impl.ClientClusterServiceImpl._sendAndReceive(ClientClusterServiceImpl.java:172)
com.hazelcast.client.spi.impl.ClientClusterServiceImpl.sendAndReceive(ClientClusterServiceImpl.java:137)
com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:42)
com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnKeyOwner(ClientInvocationServiceImpl.java:53)
com.hazelcast.client.proxy.ClientMapProxy.invoke(ClientMapProxy.java:492)
com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:83)
com.blabla.HazelcastFactory.insertUser(HazelcastFactory.java:64)

java.lang.NoSuchMethodException: com.dileky.User.<init>()
java.lang.Class.getConstructor0(Class.java:2800)
java.lang.Class.getDeclaredConstructor(Class.java:2043)
com.hazelcast.nio.ClassLoaderUtil.newInstance(ClassLoaderUtil.java:54)
com.hazelcast.nio.ClassLoaderUtil.newInstance(ClassLoaderUtil.java:50)
com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:103)
com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:36)
com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:59)
com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:218)
com.hazelcast.client.spi.impl.ClientClusterServiceImpl._sendAndReceive(ClientClusterServiceImpl.java:172)
com.hazelcast.client.spi.impl.ClientClusterServiceImpl.sendAndReceive(ClientClusterServiceImpl.java:137)
com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:42)
com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnKeyOwner(ClientInvocationServiceImpl.java:53)
com.hazelcast.client.proxy.ClientMapProxy.invoke(ClientMapProxy.java:492)
com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:83)
com.blabla.HazelcastFactory.insertUser(HazelcastFactory.java:64)

最佳答案

从堆栈跟踪看来,您的类没有默认构造函数(没有参数的构造函数)。如果您定义了带有参数集的构造函数,则编译器不会自动创建默认构造函数,您必须显式定义它。或者,您的类或构造函数可能不是公共(public)范围的,而是包私有(private)或私有(private)的。

public class User {

// Fields

public User() {
}

// Getters / Setters
}

关于map - Hazelcast IMap.get() 抛出 HazelcastSerializationException,但 IMap.put() 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206326/

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