我有一个使用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