I have a Spring App working with websocket
I can do the connection and send messages etc.
The @Controller
is:
@MessageMapping("/ws/notification")
@SendTo("/topic/springframework.topic.websocket.reply")
public NotificationReply handleNotification(Notification notification) {
...
}
It works fine. Observe the parameter type, Notification
, it is a custom object.
Now because audit purposes I have the following:
@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);
}
I get:
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
I understand that [B
is a byte[]
.
Thus exists an easy way with the current Spring Framework
API to do a cast for the Message<?>
payload to a specific POJO?.
Remember to be applied within the class that it is extending from ChannelInterceptorAdapter