Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring websocket @messagemapping de-serialization issue java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast

I am writing a spring websocket application with StompJS on the client side.

On the client side I am intending to send a List of objects and on the server side when it is mapping into java object, it converts itself into a LinkedHashMap

My client side code is

function stomball() {
         stompClient.send("/brkr/call", {}, JSON.stringify(listIds));
     }

Listids looks like

[{
    "path": "/a/b/c.txt",
    "id": 12
}, {
    "path": "/a/b/c/d.txt",
    "id": 13
}]

List Id object looks like

public class ListId {

    private String path;

    private Long id;

    //getters and setters...
}

The Controller looks like this

@MessageMapping("/call" )
@SendTo("/topic/showResult")
public RetObj process(List<ListId> listIds) {
   if (!listIds.isEmpty()) {
        for(ListId listId: listIds) {

        }
}

So I get a java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.blah.ListId

However when I do the same with normal Spring Controller with RestMapping it works fine, Is there anything with springs MessageMapping annotation that maps objects to java differently than the traditional way I am not sure why is not casting to ListID

like image 376
Nikhil Das Nomula Avatar asked Oct 30 '22 13:10

Nikhil Das Nomula


1 Answers

I changed it from a List to an Array and it works! Here is what I did

@MessageMapping("/call" )
@SendTo("/topic/showResult")
public RetObj process(ListId[] listIds) {
   if (!listIds.isEmpty()) {
        for(ListId listId: listIds) {

       }
}

Thanks to this question ClassCastException: RestTemplate returning List<LinkedHashMap> instead of List<MymodelClass>

like image 128
Nikhil Das Nomula Avatar answered Nov 15 '22 05:11

Nikhil Das Nomula