我创建一个@ServerEndpoint
和Decoders
和Encoders
。
服务器给我一个错误:java.lang.IllegalArgumentException: argument type mismatch
当我使用session.sendObject(message)
服务器发送对象时
我戴上护目镜,但找不到问题。这是我的serverEndpoint
@ServerEndpoint(value="/room"
, encoders = MessageEncoder.class
, decoders = MessageDecoder.class)
public class myServerEndpoint
{
@OnMessage
public void onMessage(Message message, Session session)
{
//some code to proccess message
}
}
这是我的 MessageDecoder:
public class MessageDecoder implements Decoder.Text<Message>
{
public Message decode(String str) throws DecodeException
{
JsonObject obj = Json
.createReader(new StringReader(str)).readObject();
Message msg = new Message();
msg.setMessage(obj.getString("message"));
msg.setSender(obj.getString("sender"));
msg.setReceivedDate((Date) obj.get("receivedDate"));
return msg;
}
//... and other Methods
}
这是我的Message.class
:
public class Message implements Serializable
{
private String message;
private String sender;
private Date receivedDate;
public Message() {}
//setters and getters
}
这是我的客户代码:
public static void main(String[] args)
{
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container
.connectToServer(WSClient.class,
URI.create("ws://localhost:8080/mySocket/room"));
Message msg = new Message();
msg.setMessage("msg");
msg.setSender("me");
msg.setReceivedDate(new Date());
session.getBasicRemote().sendObject(msg);
}
当我尝试将对象发送到服务器和服务器说时出现问题java.lang.IllegalArgumentException: argument type mismatch
。
什么是问题?