无法反序列化类型的值
我尝试创建交互式应用程序并使用套接字。 我通过邮递员在套接字中发送数据,但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)
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,使用发送请求的工具(我想是邮递员),您将其作为原始文本发送。因此反序列化器期望它是一个简单的字符串,因此它不能将其转换为复杂的对象。
您应该通知控制器它收到的是 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 aEvent
object.To do so, use the following header in your request you make
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.