Spring websocket:如何在 ChannelInterceptorAdapter 中轻松地将 Message<?> 的有效负载转换为 POJO
Spring websocket: how convert easily the Message<?>'s payload to POJO in ChannelInterceptorAdapter
我有一个 Spring 使用 websocket 的应用程序
我可以进行连接和发送消息等
@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 的简单方法?
记得在 class 中应用它是从 ChannelInterceptorAdapter
延伸出来的
AbstractMessageBrokerConfiguration
提供了这个 bean:
@Bean
public CompositeMessageConverter brokerMessageConverter() {
您需要将此 bean 注入您的 MessageChannelInterceptorAdapter
并调用其 fromMessage(Message<?> message, Class<?> targetClass)
:
Notification notification = (Notification) this.messageConverter.fromMessage(message, Notification.class);
我有一个 Spring 使用 websocket 的应用程序
我可以进行连接和发送消息等
@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 的简单方法?
记得在 class 中应用它是从 ChannelInterceptorAdapter
AbstractMessageBrokerConfiguration
提供了这个 bean:
@Bean
public CompositeMessageConverter brokerMessageConverter() {
您需要将此 bean 注入您的 MessageChannelInterceptorAdapter
并调用其 fromMessage(Message<?> message, Class<?> targetClass)
:
Notification notification = (Notification) this.messageConverter.fromMessage(message, Notification.class);