使用@sendto 将我的自定义对象数据推送到消息队列
Pushing my custom object data to a message queue using @sendto
我正在使用 spring-通过 websockets 进行消息传递。目前我有一个 @MessageMapping
控制器来从 websocket 客户端获取数据。
@MessageMapping("/path1")
@SendTo("/queue/path1-data")
public BrokerPacket myController(String message) throws Exception {
return new BrokerPacket(message, "1234", "5677");
}
我的 BrokerPacket 看起来像:
public class BrokerPacket {
private String data;
private String value1;
private String value2;
public BrokerPacket(String data, String value1, String value2) {
this.data = data;
this.value1 = value1;
this.value2 = value2;
}
}
这给了我一个 MessageConversionException
org.springframework.messaging.converter.MessageConversionException: Unable to convert payload with type='com.company.dto.BrokerPacket', contentType='null', converter=[CompositeMessageConverter[converters=[org.springframework.messaging.converter.StringMessageConverter@2f9f7783, org.springframework.messaging.converter.ByteArrayMessageConverter@312048e, org.springframework.messaging.converter.MappingJackson2MessageConverter@7764e617]]]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.doConvert(AbstractMessageSendingTemplate.java:186) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:149) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:128) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SendToMethodReturnValueHandler.handleReturnValue(SendToMethodReturnValueHandler.java:188) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:107) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:527) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:495) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:88) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:473) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:409) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:138) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]
现在,我最终使用 GSON 解析器对自定义对象进行字符串化,return String
。
@MessageMapping("/path1")
@SendTo("/queue/path1-data")
public String myController(String message) throws Exception {
Gson gson = new Gson();
return gson.toJson(new BrokerPacket(message, "sdf", "asdf"));
}
我正在使用 spring-通过 websockets 进行消息传递。目前我有一个 @MessageMapping
控制器来从 websocket 客户端获取数据。
@MessageMapping("/path1")
@SendTo("/queue/path1-data")
public BrokerPacket myController(String message) throws Exception {
return new BrokerPacket(message, "1234", "5677");
}
我的 BrokerPacket 看起来像:
public class BrokerPacket {
private String data;
private String value1;
private String value2;
public BrokerPacket(String data, String value1, String value2) {
this.data = data;
this.value1 = value1;
this.value2 = value2;
}
}
这给了我一个 MessageConversionException
org.springframework.messaging.converter.MessageConversionException: Unable to convert payload with type='com.company.dto.BrokerPacket', contentType='null', converter=[CompositeMessageConverter[converters=[org.springframework.messaging.converter.StringMessageConverter@2f9f7783, org.springframework.messaging.converter.ByteArrayMessageConverter@312048e, org.springframework.messaging.converter.MappingJackson2MessageConverter@7764e617]]]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.doConvert(AbstractMessageSendingTemplate.java:186) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:149) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:128) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SendToMethodReturnValueHandler.handleReturnValue(SendToMethodReturnValueHandler.java:188) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:107) ~[spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:527) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:495) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:88) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:473) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:409) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:138) [spring-messaging-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]
现在,我最终使用 GSON 解析器对自定义对象进行字符串化,return String
。
@MessageMapping("/path1")
@SendTo("/queue/path1-data")
public String myController(String message) throws Exception {
Gson gson = new Gson();
return gson.toJson(new BrokerPacket(message, "sdf", "asdf"));
}