Spring websocket: how convert easily the Message<?>'s payload to POJO in ChannelInterceptorAdapter

夙愿已清 提交于 2019-12-02 22:11:52

问题


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


回答1:


The AbstractMessageBrokerConfiguration provides this bean:

@Bean
public CompositeMessageConverter brokerMessageConverter() {

You need to inject this bean into your MessageChannelInterceptorAdapter and call its fromMessage(Message<?> message, Class<?> targetClass):

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


来源:https://stackoverflow.com/questions/50419131/spring-websocket-how-convert-easily-the-messages-payload-to-pojo-in-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!