1

我创建一个@ServerEndpointDecodersEncoders

服务器给我一个错误: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

什么是问题?

4

1 回答 1

0

我终于找到了我的问题。

类中的willDecode函数MessageTextDecode是 returnfalse并且serverEndpoint是 say argument type mismatch

true如果字符串没有问题,该函数应该返回。

public boolean willDecode(String arg0) 
{
    return true;  // in previous my code this returned false 
}
于 2014-10-31T06:51:11.780 回答