Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly convert List of specific objects to Gson?

I am working on Spring MVC project. I am using Hibernate. I want to use AJAX with jQuery to get some JSONs from my Spring Controllers. Unfortunately when I was implementing Gson methods in my application I have got an error:

java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: 
org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

Which adapter I have to use and in which way? The error has occurred on the last line of the method:

public String messagesToJson(List<Message> messages) {
    Gson gson = new Gson();     
    List<Message> synchronizedMessages = Collections.synchronizedList(messages);
    return gson.toJson(synchronizedMessages, ArrayList.class);
}

This is my Message class I am using in my Spring MVC project with Hibernate:

@Entity
@Table(name = "MESSAGES", schema = "PUBLIC", catalog = "PUBLIC")
public class Message implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private int messageId;
    private User users;
    private String message;
    private Date date;

    //Constructor, getters, setters, toString
}

EDIT

I am wondering: my Message object is proxied or the whole List<Message>? I am getting the list of messages in this way:

public List<Message> findAllUserMessages(String username) {
    Query query = entityManager.createQuery("from Message where username = :username order by date desc")
            .setParameter("username", username);

    @SuppressWarnings("unchecked")
    List<Message> messages = query.getResultList();
    return messages;
}

EDIT 2

No, my List<Message> object isn't proxied.

like image 658
woyaru Avatar asked Jul 17 '12 07:07

woyaru


1 Answers

I have resolved my problem. The assumption about HibernateProxy objects seemed to be very probable. However everything has started to work properly when I have read carefully my error. Finally I have registered type adapter in this way:

public String messagesToJson(List<Message> messages) {  
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.registerTypeAdapter(Message.class, new MessageAdapter()).create();
    return gson.toJson(messages);
}   

Any my MessageAdapter class looks like:

public class MessageAdapter implements JsonSerializer<Message> {

    @Override
    public JsonElement serialize(Message message, Type type, JsonSerializationContext jsc) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("message_id", message.getMessageId());
        jsonObject.addProperty("message", message.getMessage());
        jsonObject.addProperty("user", message.getUsers().getUsername());
        jsonObject.addProperty("date", message.getDate().toString());
        return jsonObject;      
    }
}

And thats all. Now I can get JSONs in jQuery using AJAX properly.

like image 98
woyaru Avatar answered Oct 04 '22 19:10

woyaru