代码之家  ›  专栏  ›  技术社区  ›  Manuel Jordan

Spring websocket:如何轻松转换消息<?>'s有效载荷到ChannelInterceptorAdapter中的POJO

  •  0
  • Manuel Jordan  · 技术社区  · 8 年前

    我有一个使用websocket的Spring应用程序

    我可以连接和发送消息等。

    这个 @Controller 是:

        @MessageMapping("/ws/notification")
        @SendTo("/topic/springframework.topic.websocket.reply")
        public NotificationReply handleNotification(Notification notification) {
         ...
        }
    

    它工作得很好。观察参数类型, Notification ,它是一个自定义对象。

    现在,由于审计目的,我有以下几点:

    @Component
    public class MessageChannelInterceptorAdapter extends ChannelInterceptorAdapter {
    
        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
    
            StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
    
               ....
    
            switch(stompHeaderAccessor.getCommand()) {
                case CONNECT:
                    logger.info("postSend ...");
                    logger.info("STOMP Connect");
                    break;
                ...
                case SEND:
                    logger.info("postSend ...");
                    logger.info("STOMP Send");
                    printDetails(message, stompHeaderAccessor);
                    break;
    
            ...
        } 
    
    
        private void printDetails(Message<?> message, StompHeaderAccessor stompHeaderAccessor) {
            logger.info("   {}", message.toString());
            logger.info("       Payload: {}", message.getPayload());
            logger.info("       Payload (String): {}", message.getPayload().toString());
            logger.info("           Payload (Class): {}", message.getPayload().getClass());
    
            if(stompHeaderAccessor.getDestination()!=null && stompHeaderAccessor.getDestination().equals("/app/ws/notification")) {
                logger.info("Aprrrr");
                Notification notification = (Notification) message.getPayload();
                logger.info("{}", notification);
            }
    

    我得到:

    ERROR o.s.w.s.m.StompSubProtocolHandler - 
    Failed to send client message to application via MessageChannel in session 555qe12a. 
    Sending STOMP ERROR to client. 
    org.springframework.messaging.MessageDeliveryException: 
    Failed to send message to ExecutorSubscribableChannel[clientInboundChannel]; 
    nested exception is java.lang.ClassCastException: 
    [B cannot be cast to com.manuel.jordan.websocket.message.Notification
    

    我明白这一点 [B 是一个 byte[]

    因此,存在一种简单的方法 Spring Framework API为 Message<?> 特定POJO?的有效载荷?。

    记住要在它所扩展的类中应用 ChannelInterceptorAdapter

    1 回复  |  直到 8 年前
        1
  •  1
  •   Artem Bilan    8 年前

    这个 AbstractMessageBrokerConfiguration 提供此bean:

    @Bean
    public CompositeMessageConverter brokerMessageConverter() {
    

    您需要将此bean注入 MessageChannelInterceptorAdapter 并称其为 fromMessage(Message<?> message, Class<?> targetClass) :

    Notification notification = (Notification) this.messageConverter.fromMessage(message, Notification.class);