Sto cercando di serializzare un proxy Java dinamico utilizzando Jackson biblioteca ma ottengo questo errore:JSON Serializzare JDK dinamica Proxy con Jackson biblioteca
public interface IPlanet {
String getName();
}
Planet implements IPlanet {
private String name;
public String getName(){return name;}
public String setName(String iName){name = iName;}
}
IPlanet ip = ObjectsUtil.getProxy(IPlanet.class, p);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(ip);
//The proxy generation utility is implemented in this way:
/**
* Create new proxy object that give the access only to the method of the specified
* interface.
*
* @param type
* @param obj
* @return
*/
public static <T> T getProxy(Class<T> type, Object obj) {
class ProxyUtil implements InvocationHandler {
Object obj;
public ProxyUtil(Object o) {
obj = o;
}
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
Object result = null;
result = m.invoke(obj, args);
return result;
}
}
// TODO: The suppress warning is needed cause JDK class java.lang.reflect.Proxy
// needs generics
@SuppressWarnings("unchecked")
T proxy = (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type },
new ProxyUtil(obj));
return proxy;
}
ottengo questa eccezione:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class $Proxy11 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS))
Il il problema sembra essere lo stesso che accade quando gli oggetti proxy in hibernate sono serializzati, ma non so come e se posso usare il modulo Jackson-hibernate per risolvere il mio problema.
UPDATE: il bug è stato risolto da Jackson 2.0.6 rilasciare
Domanda veloce su "p" qui - che tipo è? (Pianeta?). Inoltre, 'Planet' dovrebbe implementare' IPlanet': se lo è, qual è il vantaggio di Proxy? – StaxMan