我正在使用带有 websockets 的 spring-messaging。目前我有一个@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; }}这给了我一个MessageConversionExceptionorg.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]
1 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
现在,我最终使用 GSON 解析器对自定义对象进行字符串化并返回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"));
}
添加回答
举报
0/150
提交
取消