GsonSerializer HibernateProxy

6 views
Skip to first unread message

Ivo Sestren Junior

unread,
Apr 29, 2016, 4:44:05 PM4/29/16
to caelum-...@googlegroups.com
Tenho varios metodos que serializam uma consulta no JPA (Hibernate).

Acontece que quando utilizo por exemplo:
result.use(Results.json()).withoutRoot().from(url).include("dominio").serialize();

No caso o dominio é um outro objeto do JPA, carregado em modo Lazy.

Tem como criar um JsonSerializar para tratar isto e fazer o getImplementation do HibernateProxy?

Porque até tenho uma função para fazer o getImplementation, mas funciona só para o objeto principal, no caso a "url".
Para um List ou objetos que adiciono no result.include não funciona.



Cheguei a criar um conversor, mas que não esta funcionando:

public class HibernateProxyGsonConverter implements JsonSerializer<HibernateProxy> {
    @Override
    public JsonElement serialize(HibernateProxy src, Type typeOfSrc, JsonSerializationContext context) {
        Object deProxied = src.getHibernateLazyInitializer().getImplementation();
        return new Gson().toJsonTree(deProxied);
    }
}


Alguma ideia de como resolver isto?

Att.

Ivo Sestren Junior

unread,
May 3, 2016, 7:13:42 AM5/3/16
to caelum-...@googlegroups.com
Achei a solução, compartilho aqui para quem mais precisar.


public class HibernateProxyGsonConverter implements JsonSerializer<HibernateProxy> {

    public HibernateProxyGsonConverter() {
}

@Override
public JsonElement serialize(HibernateProxy proxyObj, Type arg1, JsonSerializationContext context) {
try {
Object deProxied = proxyObj.getHibernateLazyInitializer().getImplementation();
return context.serialize(deProxied);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Reply all
Reply to author
Forward
0 new messages