使用@sendto 将我的自定义对象数据推送到消息队列

我正在使用带有 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;

    }

}

这给了我一个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]


当年话下
浏览 188回答 1
1回答

繁花不似锦

现在,我最终使用 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"));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java