不能通过websocket发送对象

时间:2017-12-09 12:58:34

标签: java spring websocket

所以我试图从客户端发送一个对象 - >服务器& server->带WebSocket的客户端。从客户端 - >服务器发送对象工作正常,同时server->客户端抛出异常

  

org.springframework.messaging.converter.MessageConversionException:无法读取JSON:无法从START_OBJECT标记中反序列化java.lang.String的实例

以下是我尝试发送的课程

@Data
@AllArgsConstructor
@NoArgsConstructor
    public class TextMessage {
    private String sender;
    private String room;
    private String message;
}

这是客户端的代码

public class TelepatiClient {
    public static void main(String[] args) {
        WebSocketClient client = new StandardWebSocketClient();
        WebSocketStompClient stompClient = new WebSocketStompClient(client);
        stompClient.setMessageConverter(new MappingJackson2MessageConverter());
        stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
        String url = "ws://localhost:8000/connect";
        StompSessionHandler handler = new TelepatiSessionHandler();
        stompClient.connect(url, handler);
        new Scanner(System.in).nextLine();
    }
}

public class TelepatiSessionHandler extends StompSessionHandlerAdapter {
    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
        session.subscribe("/room/global", this);
        session.send("/test", new TextMessage("test", "test", "test"));
    }
    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        System.out.println(payload.toString());
    }
    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
        exception.printStackTrace();
        super.handleException(session, command, headers, payload, exception);
    }
}

这是服务器端的消息控制器

@Controller
public class TelepatiController {
    @MessageMapping("/test")
    @SendTo("/room/global")
    public TextMessage getMessage(TextMessage message) {
        System.out.println("get message :" + message.toString());
        return new TextMessage("test2", "test2", "test2");
    }
}

我能够运行 System.out.println("获取消息:" + message.toString()); ,但在客户端上获取消息转换异常 - 返回新TextMessage(" test2"," test2"," test2"); 。从我之前的测试中,返回一个String对象工作正常,为什么返回TextMessage对象不起作用?如何从server->客户端发送任何对象(在本例中为TextMessage)?谢谢!

2 个答案:

答案 0 :(得分:1)

问题在于内容。在这一行:

stompClient.setMessageConverter(new MappingJackson2MessageConverter());

您指示客户端使用json转换器。因此,您的客户端总是期待一个JSON对象。

但在你的测试中,在这一行:

 return new TextMessage("test2", "test2", "test2");

您正在发送纯文本。由于StompClient被抛出异常

  

org.springframework.messaging.converter.MessageConversionException

因为text plain中的消息不是JSON对象。

答案 1 :(得分:0)

我希望有人遇到同样的问题,我所做的就是告诉Topler处理程序,我订阅的内容,将返回的有效载荷类型。

这是我的stompClient的处理程序:

public class TelepatiSessionHandler extends StompSessionHandlerAdapter {
    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
        session.send("/test", new TextMessage("test", "test", "test"));
    }
    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
        exception.printStackTrace();
        super.handleException(session, command, headers, payload, exception);
    }
}

这是我订阅的主题的处理程序

  WebSocketClient client = new StandardWebSocketClient();
  WebSocketStompClient stompClient = new WebSocketStompClient(client);
  stompClient.setMessageConverter(new MappingJackson2MessageConverter());
  stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
  String url = "ws://localhost:8000/connect";
  StompSessionHandler handler = new TelepatiSessionHandler();
  StompSession session =  stompClient.connect(url, handler).get();
  session.subscribe("/room/global", new StompFrameHandler() {

      @Override
      public Type getPayloadType(StompHeaders headers) {
             return TextMessage.class;
      }

      @Override
      public void handleFrame(StompHeaders headers, Object payload) {
              TextMessage textMessage = (TextMessage) payload;
          System.out.println(textMessage.toString());
      }
  });

这是一个完整的示例: https://github.com/jaysridhar/spring-websocket-client/blob/master/src/main/java/sample/Application.java