cyden cyden - 3 months ago 42
reST (reStructuredText) Question

REST GET Response with Object containing a HashMap

I have following Rest service which queries a database, constructs multiple "Chat"-objects and returns them as an array:

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){
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:


Answer Source

You need to have both getter and setter in your POJO for the inner map.

public class Chat {
    private HashMap<String, String> msgs;

    public void setMsgs(HashMap<String, String> msgs) {
        this.msgs = msgs;

    // rest of the code ...

If you don't want to change pojo implementation for some reason, you can setup Jackson to use private fields and not getters/setters, something like this: how to specify jackson to only use fields - preferably globally

For some reason your serverside sends you

instead of 

You can probably solve it with just client side pojo changes like this:

public void setMsgs(Map<String, List<Map<String,String>>> entries){
    for (Map<String, String> entry: entries.get("entry"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download