无法反序列化类型的值

发布于 2025-01-09 05:16:15 字数 1487 浏览 0 评论 0原文

我尝试创建交互式应用程序并使用套接字。 我通过邮递员在套接字中发送数据,但SpringBoot无法反序列化它。请帮忙)

在此处输入图像描述

我的 dto

public class Event {
    @JsonProperty("eventType")
    private String eventType;

    public Event(String eventType) {
        this.eventType = eventType;
    }

    public String getEventType() {
        return eventType;
    }

    @Override
    public String toString() {
        return "Event{" +
                "eventType='" + eventType + '\'' +
                '}';
    }
}

控制器

@Controller
public class GameController {

    @MessageMapping("/emit")
    @SendTo("/topic/events")
    public Event emitEvent(Event event) {
        return event;
    }
}

错误

2022-02-23 16:19:41.359 ERROR 12468 --- [nio-8080-exec-6] s.w.s.s.t.s.WebSocketServerSockJsSession : Broken data received. Terminating WebSocket connection abruptly

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Ljava.lang.String;` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{
    "eventType": "TestData"
}"; line: 1, column: 1]

更新 1:

在此处输入图像描述

I try to create interactive apllication and working with sockets.
I send data by postman in socket, but SpringBoot cannot deserialize it. Pleasr, help)

enter image description here

My dto

public class Event {
    @JsonProperty("eventType")
    private String eventType;

    public Event(String eventType) {
        this.eventType = eventType;
    }

    public String getEventType() {
        return eventType;
    }

    @Override
    public String toString() {
        return "Event{" +
                "eventType='" + eventType + '\'' +
                '}';
    }
}

Controller

@Controller
public class GameController {

    @MessageMapping("/emit")
    @SendTo("/topic/events")
    public Event emitEvent(Event event) {
        return event;
    }
}

Error

2022-02-23 16:19:41.359 ERROR 12468 --- [nio-8080-exec-6] s.w.s.s.t.s.WebSocketServerSockJsSession : Broken data received. Terminating WebSocket connection abruptly

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Ljava.lang.String;` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{
    "eventType": "TestData"
}"; line: 1, column: 1]

Update 1:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-01-16 05:16:15

问题是,使用发送请求的工具(我想是邮递员),您将其作为原始文本发送。因此反序列化器期望它是一个简单的字符串,因此它不能将其转换为复杂的对象。

您应该通知控制器它收到的是 Json 请求,以便它可以使用正确的消息正文读取器解析它并将其转换为事件对象。

为此,请在您发出的请求中使用以下标头

Content-Type: application/json

此外,这里的问题似乎是您用来发送消息的 url。此 url 似乎并不代表应以 emit 结尾的控制器端点。我认为您将请求直接发送到消息代理而不是应用程序的控制器。

The problem is that with the tool that you send the request (I suppose it is postman) you send it as raw text. So the desirializer expects it to be a simple String and so it can not convert it into a complex object.

You should have informed the controller that what it receives is a Json request, so that it can parse it with the correct message body reader and convert it into a Event object.

To do so, use the following header in your request you make

Content-Type: application/json

Also the problem here seems to be the url that you use to send the message. This url does not seem to represent the controller endpoint which should end with emit. I think you send the request directly to the message broker and not the controller of the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文