I know that this question has already been answered, but here is another solution.
To get Jackson to convert your JSON array to a list, you have to wrap it in another object and serialize / deserialize this object.
So, you will need to send the next JSON to the server
{ list: [ { "path": "/a/b/c.txt", "id": 12 }, { "path": "/a/b/c/d.txt", "id": 13 } ] }
The list is wrapped in another object.
Below is a shell class
class ServiceRequest { private List<ListId> list; public List<ListId> getList() { if (list == null) { list = new ArrayList<ListId>(); } return list; } }
and the message method will become
@MessageMapping("/call" ) @SendTo("/topic/showResult") public RetObj process(ServiceRequest request) { List<ListId> listIds = request.getList(); if (!listIds.isEmpty()) { for(ListId listId: listIds) { } } }
Test code
import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.map.ObjectMapper; public class TestJackson { public static void main(String[] args) throws Exception { System.out.println("Started"); String json = "{\"list\":[{\"path\":\"/a/b/c.txt\",\"id\":12},{\"path\":\"/a/b/c/d.txt\",\"id\":13}]}"; ObjectMapper mapper = new ObjectMapper(); ServiceRequest response = mapper.readValue(json.getBytes("UTF-8"), ServiceRequest.class); for(ListId listId : response.getList()) { System.out.println(listId.getId() + " : " + listId.getPath()); } } public static class ServiceRequest { private List<ListId> list; public List<ListId> getList() { if (list == null) { list = new ArrayList<ListId>(); } return list; } } public static class ListId { private String path; private String id; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getId() { return id; } public void setId(String id) { this.id = id; } } }
Test output
Started 12 : /a/b/c.txt 13 : /a/b/c/d.txt
source share