I have following Rest service which queries a database, constructs multiple "Chat"-objects and returns them as an array:
@GET
@Path("/getChats")
@Produces(MediaType.APPLICATION_JSON)
public Chat[] getChats(@QueryParam("userId") String userId){
ArrayList<Chat> chats = getChatsDB(userId);
Chat[] chatAr = new Chat[chats.size()];
return chats.toArray(chatAr);
}
The "Chat"-class is a POJO:
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Chat {
private String userId1;
private String userId2;
private HashMap<String, String> msgs;
public Chat() {
msgs = new HashMap<>();
}
public String getUserId1() {
return userId1;
}
public void setUserId1(String userId1) {
this.userId1 = userId1;
}
public String getUserId2() {
return userId2;
}
public void setUserId2(String userId2) {
this.userId2 = userId2;
}
public void addMsg(String date, String msg){
msgs.put(date, msg);
}
public HashMap<String, String> getMsgs() {
return msgs;
}
}
The client code for getting this chat objects is :
public static Chat[] getChats() {
Chat[] chats = null;
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
String chatUrl = url+"getChats?userId="+user.getId();
chats = restTemplate.getForObject(chatUrl, Chat[].class);
for(Chat c: chats){
System.out.println(c.getUserId1());
System.out.println(c.getUserId2());
for(Map.Entry<String,String> e : c.getMsgs().entrySet()){
System.out.println(e.getKey() + e.getValue());
}
}
return chats;
The client recieves the chat objects, but without the HashMap with the messages. c.getUserId1 and c.getUserId2 return the correct values, but the HashMap is empty. This problem is only on the client side. The chat-objects in the servicemethod getChats(@QueryParam("userId") String userId) have the correct values in the HashMap.
Opening the link in the browser shows this:
[{"userId1":"414","userId2":"12"}]