STOMP 中的对象 react.js - Spring 启动

Object in STOMP react.js - Spring Boot

我正在尝试将一些数据从 React.js 发送到 Spring 使用 STOMP 引导。

我在服务器端有这个设置:

@MessageMapping("/send")
    @SendTo("/topic/public")
    public String send(@Payload Notification data) {
        try {
            logger.debug("****send****");
            logger.debug(data.getData());
            logger.debug("****send****");
            Thread.sleep(1000 * 10);
            return "sending back" + data.getData();
        } catch (Exception e) {
            logger.debug("---" + e.getMessage() + "---");
            return "";
        }
    }

在客户端:

        const Stomp = require('stompjs');
        var SockJS = require('sockjs-client');
        SockJS = new SockJS('http://localhost:8080//ws');
        this.state.stompClient = Stomp.over(SockJS);
        this.state.stompClient.send("/app/send", {}, {"data": this.state.data});

客户端这个returns我[对象对象]

并且在服务器端:无法读取JSON:无法反序列化......的实例START_ARRAY 令牌

但是当我只发送 String

时这真的很好用

完整消息:

org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot deserialize instance of `com.mua.telapoka.pojo.Notification` out of START_ARRAY token
 at [Source: (byte[])"[object Object]"; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.mua.telapoka.pojo.Notification` out of START_ARRAY token
 at [Source: (byte[])"[object Object]"; line: 1, column: 1]
    at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:235) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.converter.AbstractMessageConverter.fromMessage(AbstractMessageConverter.java:197) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.converter.CompositeMessageConverter.fromMessage(CompositeMessageConverter.java:70) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver.resolveArgument(PayloadMethodArgumentResolver.java:138) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:148) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:565) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:511) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:94) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:520) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:454) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:144) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.mua.telapoka.pojo.Notification` out of START_ARRAY token
 at [Source: (byte[])"[object Object]"; line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1464) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1238) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:190) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3529) ~[jackson-databind-2.11.0.jar:2.11.0]
    at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:219) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    ... 15 common frames omitted

因为 Stomp send(destination, headers, body) 需要 String 类型的主体。所以你需要通过 JSON.stringify({data: this.state.data}) 转换为 String。当然,您的服务器应该包含匹配的模型。