Estoy tratando de serializar un proxy de Java usando la biblioteca dinámica Jackson pero me sale este error:JSON Serializar JDK proxy dinámico con la biblioteca de Jackson
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;
}
consigo esta excepción:
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))
El El problema parece ser el mismo que ocurre cuando los objetos con proxy hibernación son serializados, pero no sé cómo y si puedo usar el módulo Jackson-hibernate para resolver mi problema.
ACTUALIZACIÓN: El error se resuelve de Jackson 2.0.6 liberar
Pregunta rápida sobre 'p' aquí, ¿de qué tipo es? (¿Planeta?). Además, ¿debería 'Planet' implementar' IPlanet': si es así, ¿cuál es el beneficio de Proxy? – StaxMan